如何记录 cron 执行所花费的时间。

Ste*_*wie 3 linux logging cron

我有一个 cron 设置为每分钟运行一次

* * * * * /usr/php /my/location/script.php 
Run Code Online (Sandbox Code Playgroud)

现在,我使用time函数来测量脚本执行时间。所以,运行

console$ time /usr/php /my/location/script.php 
Run Code Online (Sandbox Code Playgroud)

产出

real    0m0.000s
user    0m0.000s
sys     0m0.000s
Run Code Online (Sandbox Code Playgroud)

但它不适用于这样的 cron:

* * * * * time /usr/php /my/location/script.php 2>&1 >> my_log_file
Run Code Online (Sandbox Code Playgroud)

它也不适用于命令行

console$ time /usr/php /my/location/script.php >> my_log_file
Run Code Online (Sandbox Code Playgroud)

在以上两个示例中,time 函数实际上计算写入 my_log_file 所花费的时间,而不是将其输出写入日志文件。在脚本中添加代码并记录 STD 输出不是一个选项。

mdp*_*dpc 9

关于什么:

 * * * * *    (time /usr/php /my/location/script.php) >>my_log_file 2>&1
Run Code Online (Sandbox Code Playgroud)

  • parens 启动一个shell,因此您在shell 下运行parens 之间的内容......并将其输出发送到重定向的位置。 (3认同)