doushuhuai7247 2016-01-22 16:47
浏览 347
已采纳

PHP exec()和运行shell脚本给出了不同的结果

I have script.sh that sends a request to some URL and prints the execution time using whet. Here is the source code:

time wget http://some.url

When I run it from command line, I see time result with an accuracy of 3 decimal places:

real    0m0.584s
user    0m0.000s
sys     0m0.002s

But, when I run this SH using PHP function exec("script.sh", $output) - , I see time result with an accuracy of only 2 decimal places:

0.00user 0.00system 0:00.32elapsed 0%CPU (0avgtext+0avgdata 3788maxresident)k

How can I get the same result, as I do see in command line?

  • 写回答

1条回答 默认 最新

  • drgovyk64676 2016-01-22 20:28
    关注

    The answer is in the manual:

    Note: some shells (e.g., bash(1)) have a built-in time command that provides less functionality than the command described here. To access the real command, you may need to specify its pathname (something like /usr/bin/time).

    You're getting the increased precision from the bash builtin; if you run /usr/bin/time from the shell you will see two decimal places as well.

    Unfortunately, time is a special case, in that it's a keyword and not actually a builtin command. Otherwise bash's builtin command could be used to force it.

    So it looks like you're stuck with two decimal places unless you want to try a different method:

    start_time=`date +%s%N`
    wget http://some.url
    end_time=`date +%s%N`
    bc -l <<< "scale=4;($end_time - $start_time) / 1000000000"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败