Random pause before executing cron task


Before executing a job in cron, I need to pause randomly. I found a construction like this:

0 10 * * * sleep $(( RANDOM % 30 )); wifi down

But it doesn't work for me. In the logs, I see that there was no pause:

Sun Sep 13 10:00:00 2020 cron.info crond[11929]: USER root pid 11940 cmd sleep $(( RANDOM % 30 )); wifi down
Sun Sep 13 10:00:01 2020 daemon.notice hostapd: wlan0: AP-DISABLED

How can i do this?

1 Like
$(awk 'BEGIN{srand();print int(rand()*30)}')
1 Like

I'm not familiar with awk, does this construct do the same thing?

Pretty much the same.

Thank you very much!

1 Like

For three days of use, I noticed that the pause never exceeded 1 hour, despite the set 3 hours. Any ideas and solutions?

No, I am indicating the time in seconds.

1 Like

Can you show the current config?

0 0 * * * sleep $(awk 'BEGIN{srand();print int(rand()*10800)}'); wifi down
0 5 * * * sleep $(awk 'BEGIN{srand();print int(rand()*7200)}'); wifi up

1 Like

Try the following sleep version instead of the default BusyBox applet:

Do I only need to install this package?

1 Like

Yep, then you can verify:

> readlink -f $(type -p sleep)

> sleep --help
Usage: sleep NUMBER[SUFFIX]...
  or:  sleep OPTION
Pause for NUMBER seconds.  SUFFIX may be 's' for seconds (the default),
'm' for minutes, 'h' for hours or 'd' for days.  Unlike most implementations
that require NUMBER be an integer, here NUMBER may be an arbitrary floating
point number.  Given two or more arguments, pause for the amount of time
specified by the sum of their values.

Now do I need to rewrite the cron rules? But I don't find examples of what I need.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.