Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a cron job randomly for every one hour

Tags:

linux

cron

ubuntu

I want a cronjob to run every one hour randomly. (i.e if the first job runs at 58 minutes,the second job should run at 47 minutes and the third one at 52 minutes and so on) But this should run randomly for everyone hour. Is there a way to do this?

like image 941
Harnish Avatar asked Dec 14 '22 06:12

Harnish


1 Answers

You could run a job every hour, on the hour, that sleeps up to 3,599 seconds and then executes your script:

0 * * * * /path/to/perl -e 'sleep int rand 3600' && /path/to/yourScript

Or, using PHP if you prefer that to Perl:

0 * * * * /path/to/php -r 'sleep(rand(0,3599));' && /path/to/yourScript

You can find the path to Perl with:

which perl

likewise for PHP:

which php
like image 118
Mark Setchell Avatar answered Dec 16 '22 20:12

Mark Setchell