How to make cron run something every "N" minute, where n% 5 == 1?

I know that I can run something every five minutes in cron using a line, for example:

*/5 * * * * /my/script 

What if I do not want it to work at 12:00, 12:05, 12:10, but rather at 12:01, 12:06, 12:11, etc.? I think I can do this:

  1,6,11,16,21,26,31,36,41,46,51,56 * * * * /my/script 

... but it's ugly. Is there a more elegant way to do this?

+41
linux unix cron sysadmin
Jan 22 '09 at 22:07
source share
5 answers
 1-56/5 * * * * /my/script 

This should work on vixiecron, I'm not sure about other implementations.

+73
Jan 22 '09 at 22:57
source share

Use your first schedule:

 */5 * * * * /my/script 

And add this to the top of your script:

 sleep 60 

(Yes, this is a joke)

+16
Jan 22 '09 at 10:15
source share

This is a pretty old topic, however, since a lot of time has passed, there are several more options. One of them is not to use cron at all and use systemd timers. Using them, you get a higher degree of detail than seconds, as well as many other options.

Additional information is available here https://wiki.archlinux.org/index.php/Systemd/Timers

for example, to run the adhoc command

 # systemd-run --on-calendar="*:1/5" /bin/touch /tmp/foo2 Running timer as unit run-r31335c4878f24f90b02c8ebed319ca60.timer. Will run service as unit run-r31335c4878f24f90b02c8ebed319ca60.service. # systemctl status run-r31335c4878f24f90b02c8ebed319ca60.timer ● run-r31335c4878f24f90b02c8ebed319ca60.timer - /bin/touch /tmp/foo2 Loaded: loaded Transient: yes Drop-In: /run/systemd/system/run-r31335c4878f24f90b02c8ebed319ca60.timer.d └─50-Description.conf, 50-OnCalendar.conf, 50-RemainAfterElapse.conf Active: active (waiting) since Wed 2017-10-25 09:05:13 UTC; 40s ago # ls -l /tmp/foo* -rw-r--r-- 1 root root 0 Oct 25 09:06 /tmp/foo2 # sleep 300; ls -l /tmp/foo* -rw-r--r-- 1 root root 0 Oct 25 09:11 /tmp/foo2 # date; ls -l /tmp/foo2 Wed Oct 25 09:21:42 UTC 2017 -rw-r--r-- 1 root root 0 Oct 25 09:21 /tmp/foo2 

edit: these types of timers will not be saved on reboot if you want them to create the correct service file with the corresponding oncalendar line

0
Oct 25 '17 at 9:24 on
source share

I would create a new script "delaystart" that takes the sleep period as the first parameter, and the script as the rest. I would make a script check the crontab line for a line using a script and only run the script if the line is not commented out. This makes it reusable, and ps does not tell the script how to work when it really is not.

 #!/bin/bash sleeptime=$1 sleep ${sleeptime} shift if ( ! crontab -l | grep -e '#.+delaystart '${sleeptime} $* ) then $* fi 
-one
Sep 24 '09 at 21:32
source share

The sean.bright joke made me think ... why not use ...

 * * * * * /my/script 

... and inside the script do this ...

 #!/bin/bash export WHEN=`date '+%M'` echo $WHEN export DOIT=`echo "$WHEN % 5" | bc` echo $DOIT if [ $DOIT != 0 ] ; then echo "ha ha ha" fi echo "done" 

... kludge ... maybe, but as ugly as crontab ... I don't know.

-5
Jan 22 '09 at 22:34
source share



All Articles