我有一个cron设置每分钟运行
* * * * * /usr/php /my/location/script.php
现在,我使用时间函数来衡量脚本执行时间。 所以,跑步
console$ time /usr/php /my/location/script.php
输出
real 0m0.000s user 0m0.000s sys 0m0.000s
但是这不适用于这样的cron:
* * * * * time /usr/php /my/location/script.php 2>&1 >> my_log_file
它在命令行上也不起作用
console$ time /usr/php /my/location/script.php >> my_log_file
在上面的两个例子中,时间函数实际上是计算写入my_log_file所用的时间,而不是将其输出写入日志文件。 在脚本中添加代码并loggingSTD OUTPUT不是一个选项。
关于什么:
* * * * * (time /usr/php /my/location/script.php) >>my_log_file 2>&1
问题是使用内置的time shell与time二进制。 使用内build,即使正确redirect也不起作用:
$ time pwn >> foo 2>&1 real 0m0.003s user 0m0.000s sys 0m0.002s $ cat foo /tmp
使用二进制作品:
$ /usr/bin/time pwd >> foo 2>&1 $ cat foo /tmp 0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 564maxresident)k 64inputs+8outputs (1major+175minor)pagefaults 0swaps
使用GNU时间,您可以使用-o和-a选项而不是shellredirect:
$ /usr/bin/time -o foo -a pwd /tmp $ cat foo 0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 564maxresident)k 0inputs+0outputs (0major+179minor)pagefaults 0swaps
内置的cron守护进程function如何?
检查你的cron守护进程的手册页。 许多人有一个“-L”参数指定一个日志级别。 例如在Ubuntu(即Debian)上:
-L loglevel Sets the loglevel for cron. The standard logging level (1) will log the start of all the cron jobs. A higher loglevel (2) will cause cron to log also the end of all cronjobs, which can be useful to audit the behaviour of tasks run by cron. Logging will be disabled if the loglevel is set to zero (0).
我可能会尝试把
/usr/bin/time /usr/php /my/location/script.php
进入一个脚本,然后从你的cron作业调用该脚本。
另外,你的php二进制文件真的在/ usr中吗? 或/ usr / bin?
似乎time是bash内部(至less在我的系统上)。 以下适用于我: /bin/bash -c 'time ls' > out.std 2> out.err 。 time输出将在stderr(out.err文件)中。