Crontab | random execution time

104 views
Skip to first unread message

Jos Chrispijn

unread,
Mar 7, 2022, 8:37:37 AM3/7/22
to FreeBSD Mailing List
Dear list,
I am trying to run a cron job, starting once every two hours and at
random minute  in the half our of that cron scheme:

SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/etc
0~30   */2     *       *       *       root /root/cronjobs/run_myjob

Unfortunately it is running exactly on the hour instead of random
between 0-30 minutes.
Can you pls advice how to make this work?

Thanks
Jos Chrispijn



Steve O'Hara-Smith

unread,
Mar 7, 2022, 9:16:19 AM3/7/22
to Jos Chrispijn, FreeBSD Mailing List
On Mon, 7 Mar 2022 14:36:19 +0100
Jos Chrispijn <bsd...@cloudzeeland.nl> wrote:

> Dear list,
> I am trying to run a cron job, starting once every two hours and at
> random minute  in the half our of that cron scheme:
>
> SHELL=/bin/sh
> PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/etc
> 0~30   */2     *       *       *       root /root/cronjobs/run_myjob
>
> Unfortunately it is running exactly on the hour instead of random
> between 0-30 minutes.

I'd expect that to run once a minute for the first thirty minutes
of even numbered hours.

The usual way to achieve what you want is to add a random sleep in
front of the job, or just rely on cron's jitter feature which isn't usually
set for that much spread and affects every job.

--
Steve O'Hara-Smith <st...@sohara.org>

Arthur Chance

unread,
Mar 7, 2022, 9:22:57 AM3/7/22
to Steve O'Hara-Smith, Jos Chrispijn, FreeBSD Mailing List
That line has a '~' in it which isn't a documented form. I suspect cron
reads it as '0'.

To do a random sleep of 0-30 minutes add

sleep `jot -r 1 0 1800`

to the start of your command.

--
All network cabling aspires to the condition of macramé.

Jos Chrispijn

unread,
Mar 7, 2022, 9:34:55 AM3/7/22
to Arthur Chance, Steve O'Hara-Smith, FreeBSD Mailing List
Arthur Chance:

> sleep `jot -r 1 0 1800`
> to the start of your command.

Thanks, that works!

--
With two feet on the ground you can't make any step forward



Reply all
Reply to author
Forward
0 new messages