Linux – How to record time taken by a cron to execute.

cronlinuxlogging

I have a cron set to run every minute

* * * * * /usr/php /my/location/script.php 

Now, I use time function to measure script execution time. So, running

console$ time /usr/php /my/location/script.php 

outputs

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

But it doesn't work with cron like this:

* * * * * time /usr/php /my/location/script.php 2>&1 >> my_log_file

neither does it work on command line

console$ time /usr/php /my/location/script.php >> my_log_file

In both of the above examples, the time function actually calculates the time taken to write to my_log_file, instead of writing its output to the log file.
Adding code in the script and recording STD OUTPUT is NOT AN OPTION.

Best Answer

What about:

 * * * * *    (time /usr/php /my/location/script.php) >>my_log_file 2>&1