Search code examples
linuxcron

How to randomly run crontab


I have a Python script and would like to automated execute with crontab in Raspberry Pi. I did so many search to do this but I can't find the answer. Below is my command add to crontab.

SHELL=/bin/bash  
10 * * * * sleep $((RANDOM \%600)) && myscript.py

I would like the script be executed every 10 minutes but I want to randomly run it. What I understand is the sleep and RANDOM command is giving the delay process of script within given second. In this case, it should be executed somewhere within 600 seconds (10 * 60) i.e. within 10 minutes.

However, when I added this to crontab, it gives me the execution once in an hour, instead of every 10 minutes, but the exact time is random.

What is wrong in the command? or What I understand wrongly?


Solution

  • SHELL=/bin/bash  
    */10 * * * * sleep $((RANDOM \%600)) && myscript.py
    

    The way you had it, it would only run on the 10th minute of every hour. Either of the following would be ways to say every 10 minutes.

       */10 * * * * 
    

    Or

     0,10,20,30,40,50 * * * *