Linux – Problem with $RANDOM in crontab

cronlinux

I've got a strange problem with $RANDOM in cron. I wish to execute a command a random number of minutes after the cronjob fires.

This example works directly in the terminal and delays the command up to 30 seconds (replace command with whatever you want, it's actually an echo to /dev/ttyUSB0):

sleep `expr $RANDOM \% 30` ; command

If the same line is placed in crontab, the command always fires immediately without the delay:

* * * * * sleep `expr $RANDOM \% 30` ; command

If I use an expression without $RANDOM, it works fine – this makes a delay of 15 secs:

* * * * * sleep `expr 10 + 5` ; command

In other words, it seems like $RANDOM does not work in a cron.

But it is not simply because $RANDOM itself evaluates to zero, because then this should give a delay of 10:

* * * * * sleep `expr $RANDOM \% 30 + 10` ; command

I have also tried with && instread of ; but that doesn't help. In fact, then the command does not fire at all!

I could of course place the delay in a script which is then called from crontab, but that does not explain my problem and does not make me learn 🙂

It's Debian Lenny if that makes any difference.

Best Answer

cron uses the /bin/sh shell to execute tasks. In some distros, this is a symlink to dash. Neither of them supports the $RANDOM variable, which is a bash-specific extension.

  • With vixie-cron, you can put a line SHELL=/bin/bash at the top of your crontab.

  • Otherwise, you will have to settle with bash -c 'echo $RANDOM' or perl -e 'print int(rand(65535))'.

    (In the above example, 65535 is the maximum number to return. You can apply other math inside the script too.)

  • In a properly configured system, you would have been informed about this by cron itself – it always sends job output, including error messages, by email. Install a lightweight MTA.


Also, in bash, $(( )) is preferred over `expr`.

Related Question