如何让cron每N分钟运行一次,N % 5 = 1?

时间:2021-03-31 01:08:51

I know that I can have something run every five minutes in cron with a line like:

我知道我可以每隔五分钟在cron上运行一次,比如:

 */5 * * * * /my/script

What if I don't want it running at 12:00, 12:05, 12:10, but rather at 12:01, 12:06, 12:11, etc? I guess I can do this:

如果我不希望它在12点,12点05分,12点10分运行,而是在12点01分,12点06分,12点11分等等?我想我能做到:

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

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

…但这是丑陋的。有更优雅的方法吗?

5 个解决方案

#1


76  

1-56/5 * * * * /my/script

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

这应该适用于vixiecron,我不确定其他实现。

#2


16  

Use your first schedule:

用你的第一个计划:

*/5 * * * * /my/script

And add this to the start of your script:

在你的剧本开头加上:

sleep 60

(Yes, this is a joke)

(是的,这是个玩笑)

#3


0  

This is quite an old topic, however as so much time has passed there are a few other options now. One of which is not to use cron at all, and use systemd timers. Using these gives you a higher granularity than seconds along with lots of other options

这是一个相当古老的话题,但是随着时间的流逝,现在有了一些其他的选择。其中之一是不使用cron,并使用systemd计时器。使用这些选项可以提供比秒更高的粒度,以及许多其他选项

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

更多信息可以在这里获得:https://wiki.archlinux.org/index.php/systemd/timer

eg to run a 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 type of timers wont persist over reboot, if you want them to make sure you generate a proper service file, with the relevant oncalendar line

编辑:这些类型的计时器不会在重新启动时持续存在,如果您想让它们确保您生成一个适当的服务文件,并使用相关的oncalendar行

#4


-1  

I'd create a new script "delaystart" that takes a sleeping period as first parameter and the script to run as the rest. I'd make the script check the crontab line for the line with the script and only start the script if the line is not commented out. That makes it reusable, and ps won't report the script as running when it really isn't.

我将创建一个新的脚本“delaystart”,它将睡眠时间作为第一个参数,脚本作为其余参数运行。我将使脚本检查带有脚本的行的crontab行,并且只有在没有注释的行时才启动脚本。这使得它可以重复使用,并且ps不会在实际运行时报告脚本运行。

#!/bin/bash
sleeptime=$1
sleep ${sleeptime}
shift
if ( ! crontab -l | grep -e '#.+delaystart '${sleeptime} $* ) then
  $*
fi

#5


-5  

sean.bright's joke got me thinking... why not use...

肖恩。布莱特的笑话让我想到……为什么不使用……

* * * * * /my/script

...and within 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"

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

…组装的…也许吧,但是和crontab一样丑陋……我不知道。

#1


76  

1-56/5 * * * * /my/script

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

这应该适用于vixiecron,我不确定其他实现。

#2


16  

Use your first schedule:

用你的第一个计划:

*/5 * * * * /my/script

And add this to the start of your script:

在你的剧本开头加上:

sleep 60

(Yes, this is a joke)

(是的,这是个玩笑)

#3


0  

This is quite an old topic, however as so much time has passed there are a few other options now. One of which is not to use cron at all, and use systemd timers. Using these gives you a higher granularity than seconds along with lots of other options

这是一个相当古老的话题,但是随着时间的流逝,现在有了一些其他的选择。其中之一是不使用cron,并使用systemd计时器。使用这些选项可以提供比秒更高的粒度,以及许多其他选项

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

更多信息可以在这里获得:https://wiki.archlinux.org/index.php/systemd/timer

eg to run a 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 type of timers wont persist over reboot, if you want them to make sure you generate a proper service file, with the relevant oncalendar line

编辑:这些类型的计时器不会在重新启动时持续存在,如果您想让它们确保您生成一个适当的服务文件,并使用相关的oncalendar行

#4


-1  

I'd create a new script "delaystart" that takes a sleeping period as first parameter and the script to run as the rest. I'd make the script check the crontab line for the line with the script and only start the script if the line is not commented out. That makes it reusable, and ps won't report the script as running when it really isn't.

我将创建一个新的脚本“delaystart”,它将睡眠时间作为第一个参数,脚本作为其余参数运行。我将使脚本检查带有脚本的行的crontab行,并且只有在没有注释的行时才启动脚本。这使得它可以重复使用,并且ps不会在实际运行时报告脚本运行。

#!/bin/bash
sleeptime=$1
sleep ${sleeptime}
shift
if ( ! crontab -l | grep -e '#.+delaystart '${sleeptime} $* ) then
  $*
fi

#5


-5  

sean.bright's joke got me thinking... why not use...

肖恩。布莱特的笑话让我想到……为什么不使用……

* * * * * /my/script

...and within 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"

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

…组装的…也许吧,但是和crontab一样丑陋……我不知道。