3

So I have a crontab that have this line:

30 16 * * * (time sysbench --test=cpu --cpu-max-prime=20000 run) 2>> ~/cpu.out

I use this because time output goes by default to stderr and I want to redirect it to a file

When I run the command in the terminal it does write the output I need:

real X.XXXs
user X.XXXs
sys  X.XXXs

But when I run with cron it writes this to the file:

39.69 user 0.92 system 0:40.67elapsed 99%CPU (0avgtext+0avgdata 1400maxresident)k0inputs+1outputs (0major+450minor) pagefaults 0swaps

Can someone help me?

PS: In the man page there is the -o option but it is not working, if I try that I get an error

1 Answer 1

4

When you're running it in the shell, you're actually using the bash built-in function, which looks like this:

anthony@Zia:~$ time perl -e 'sleep 1'

real    0m1.003s
user    0m0.000s
sys     0m0.004s

Cron isn't using the bash built-in; it's using /usr/bin/time:

anthony@Zia:~$ /usr/bin/time perl -e 'sleep 1'
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 1800maxresident)k
0inputs+0outputs (0major+514minor)pagefaults 0swaps

The second one actually has all the information from the bash built-in, plus more. It labels "real" as "elapsed". (This is also why the -o option is not working; that's an option for /usr/bin/time, not the bash built-in).

If you need to use the bash built-in, there are two things to try:

  1. Put SHELL=/bin/bash at the top of your crontab.
  2. Change your command to explicitly call bash -c "your command here".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .