1

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.

2
  • 1
    It may be possible, but cron is intended for running commands at regular intervals, not random ones.
    – 84104
    May 23, 2012 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, 2012 at 19:30

2 Answers 2

7

You can use a static cronjob with a delay:

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

(untested but should work)

0
3

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.

0

You must log in to answer this question.

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