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.