Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Greetings I'm new to cron. Is it possible to run automated cron jobs at random minutes for every hour?

the syntax should be different every day, for example:

34,57,44,30,37,31,38,30,44,35,36,56,46,55,54,40,45,44,42,35,50,59,48,34,52,* * * * *

To my knowledge the code above will send cron jobs at 0:34, 01:57, 02:44.... to 23:52. I'm currently generating the syntax in PHP but I'm not sure how to send it to the cron command or if it's safe. Please advise how to accomplish this.

share|improve this question
1  
It may be possible, but cron is intended for running commands at regular intervals, not random ones. –  84104 May 23 '12 at 19:20
    
And certainly not possible with that syntax. That will get you VERY regular cron job kickoffs. Every minute of every hour of every day, in fact. I recommend reading the 'man 5 crontab' page. –  Magellan May 23 '12 at 19:30

2 Answers 2

up vote 7 down vote accepted

You can use a static cronjob with a delay:

0 *    * * *   user    sleep $(($RANDOM % 59))m; my_command

(untested but should work)

share|improve this answer
    
Thank you! I will try that –  Cyber Junkie May 25 '12 at 19:42

The code you supplied would run it every minute due to the unbounded wildcard at the end. The entire first field of a cron entry is simply a list of the minute values that cron will match against to check whether it should run the program.

Cron also simply does a modulus on the time values. Dividing the minutes by 11 gets cron to kick it off at :00, :11, :22, :33, :44, and :55 minute marks of each hour.

share|improve this answer
    
Thanks! I still have a lot to learn about Cron. –  Cyber Junkie May 25 '12 at 19:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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