我知道我可以有一些运行在cron每五分钟这样一行:如何让cron每分钟运行一次“N”分钟,其中n%5 == 1?
*/5 * * * * /my/script
,如果我不希望它运行在12:00,12:05,12:10什么,但而是在12:01,12:06,12:11等?我想我可以做到这一点:
1,6,11,16,21,26,31,36,41,46,51,56 * * * * /my/script
......但这是丑陋的。有没有更好的方法来做到这一点?
我知道我可以有一些运行在cron每五分钟这样一行:如何让cron每分钟运行一次“N”分钟,其中n%5 == 1?
*/5 * * * * /my/script
,如果我不希望它运行在12:00,12:05,12:10什么,但而是在12:01,12:06,12:11等?我想我可以做到这一点:
1,6,11,16,21,26,31,36,41,46,51,56 * * * * /my/script
......但这是丑陋的。有没有更好的方法来做到这一点?
1-56/5 * * * * /my/script
这应该适用于vixiecron,我不确定其他实现。
使用第一个日程表:
*/5 * * * * /my/script
并添加到您的脚本的开头:
sleep 60
(是的,这是一个笑话)
sean.bright的笑话了我想......为什么不使用...
* * * * * /my/script
...并在脚本中做到这一点...
#!/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"
...一个杂牌组装电脑......也许,但丑如crontab的......我不知道。
我会创建一个新的脚本“delaystart”,它将睡眠周期作为第一个参数,脚本以其余的方式运行。我会让脚本检查脚本的crontab行,并且只在该行没有被注释掉的情况下才启动脚本。这使得它可以重用,并且ps不会在脚本没有运行时报告脚本运行。
#!/bin/bash
sleeptime=$1
sleep ${sleeptime}
shift
if (! crontab -l | grep -e '#.+delaystart '${sleeptime} $*) then
$*
fi
这是一个相当古老的话题,但是由于这么多时间已经过去,现在还有其他一些选择。其中之一就是不要使用cron,并使用systemd定时器。使用这些给你高于秒的粒度与许多其他选项一起
更多信息,请点击这里https://wiki.archlinux.org/index.php/Systemd/Timers
例如运行即席命令
# 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
编辑:这些类型的定时器不会持续如果你希望他们确保你生成一个合适的服务文件,请在相关的oncalendar行
我实际上使用这种变化来在午夜前1秒启动一个进程。 – 2009-01-22 22:19:04
啊,沉睡的力量-1 ;-) – 2009-09-02 22:51:15
我在阅读最后一行之前就已经移动鼠标来downvote这个:) – ymihere 2011-11-23 06:42:34