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 


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.


What about:

 * * * * *    (time /usr/php /my/location/script.php) >>my_log_file 2>&1
  • You sir, saved my day. 'Just because today is a lazy day' do you mind explaining what you did ? and how did the parenthesis help? – Stewie Sep 28 '11 at 19:56
  • 3
    the parens startup a shell and thus you run the contents between the parens under a shell...and send its output to the redirected locations. – mdpc Sep 28 '11 at 20:12

How about the in-built cron daemon feature?

Check your cron daemon's man page. Many have a "-L" argument to specify a log level. Eg on Ubuntu (ie, 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).
  • This option seems promising, could you please show an example on how to use it? – nulll Feb 7 '16 at 18:34

The problem is using the time shell builtin vs. the time binary. Using the builtin it doesn't work, even with the proper redirection:

$ time pwn >> foo 2>&1

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

$ cat foo 

Using the binary works:

$ /usr/bin/time pwd >> foo 2>&1
$ cat foo 
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 564maxresident)k
64inputs+8outputs (1major+175minor)pagefaults 0swaps

With GNU time you can use the -o and -a options instead of shell redirection:

$ /usr/bin/time -o foo -a pwd
$ cat foo 
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 564maxresident)k
0inputs+0outputs (0major+179minor)pagefaults 0swaps

I would probably try putting

/usr/bin/time /usr/php /my/location/script.php 

Into a script and then calling that script from your cron job.

As an aside, is your php binary really in /usr? Or /usr/bin?

  • Nope that didn't work and my php is in /usr/bin ... I was typing this instead of copying, so missed it.:p – Stewie Sep 28 '11 at 19:35
  • How did it fail? – jdw Sep 28 '11 at 19:37
  • It didn't fail. It just doesn't write to the file, It writes to output after the command is executed. I want the times to be recorded to a log file – Stewie Sep 28 '11 at 19:39
  • 1
    Yeah..I get it. I've been noodling around here for a few trying to come up with the magic recipe. I'm sure some command line wizard will come along soon and sort it out for you. – jdw Sep 28 '11 at 19:47

it seems time is bash builtin (at least on my system). Following works for me: /bin/bash -c 'time ls' > out.std 2> out.err. Output of time would be in stderr (out.err file).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.