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

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?

图片转代码服务由CSDN问答提供 功能建议

我有 script.sh ,它向某个URL发送请求并打印执行时间 使用 whet 。 以下是源代码:

  time wget http://some.url 
   
 
 

当我运行它时 从命令行,我看到时间结果精确到3位小数:

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

但是,当我使用PHP函数 exec(“script.sh”,$ output) - 运行此SH时,我看到的时间结果只有精确度 2位小数:

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

如何在命令行中看到相同的结果?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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"
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题