如何设置 cron 每 40 分钟/25 分钟运行一次我的脚本? [英] How do set cron to run my script every 40mins/25mins?

查看:26
本文介绍了如何设置 cron 每 40 分钟/25 分钟运行一次我的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望脚本从第 40 分钟开始每 40 分钟运行一次.
所以这意味着:

I want a script to run every 40mins beginning on the 40th minute.
so that means:

00:40, 01:20, 02:00, 02:40, 03:20...

所以我向 cron 输入了这个条目:

So I made this entry to cron:

*/40 * * * * /path/to/script/foo.sh

不幸的是,这个脚本每 40 分钟运行一次:

Unfortunately this runs the script every 40th minute of the hour:

00:40, 01:40, 02:40...

我打算每 25 分钟运行一次的脚本也是如此.

The same goes with the script that I meant to run every 25mins.

我在这里遗漏了什么吗?

Am I missing something here?

答案
好的,万一你碰巧遇到同样的问题
这是我解决它的方法:

ANSWERS
Alright, in case you happen to drop by here having the same problem
here's how I solved it:

# 40mins-interval
40 0 * * * /path/foo.sh         (0)
0,40 2-22/2 * * * /path/foo.sh  (2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22)
20 1-23/2 * * * /path/foo.sh    (1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23)  


# 25mins-interval
25,50 0 * * * /path/foo.sh              (0)
0,25,50 5-20/5 * * * /path/foo.sh       (5, 10, 15, 20)
15,40 1-21/5 * * * /path/foo.sh         (1, 6, 11, 16, 21)
5,30,55 2-22/5 * * * /path/foo.sh       (2, 7, 12, 17, 22)
20,45 3-23/5 * * * /path/foo.sh         (3, 8, 13, 18, 23)
10,35 4-19/5 * * * /path/foo.sh         (4, 9, 14, 19)

注意事项:
1. 此时间表中仍会存在冲突(即:请参阅在两个时间间隔的第 0 分钟和第 10 分钟运行的时间表).
2. 脚本不会按照今天最后一次运行到第二天的确切时间间隔运行(即:25 分钟间隔今天@23:45 结束,第二天@00:25 开始).

Notes:
1. There will still be collisions in this schedule (i.e: see schedules that run on the 0th and 10th minutes on both intervals).
2. The script won't run at an exact interval from its last run today going on the next day (i.e: 25min interval ends @23:45 today, begins @00:25 next day).

推荐答案

它总是只拆分当前小时.

It always splits the current hour only.

40/40 = 1 所以它每 40 分钟运行一次.

40/40 = 1 so it runs every 40th minute of an hour.

*/5 会做 5、10、15...

*/5 would do 5, 10, 15...

你应该去更大的间隔.

在 25 分钟的间隔中做 */30 次,在 40 分钟的间隔中每 60 分钟做一次.

Do */30 for your 25 minute interval and every 60 minutes for your 40 minutes interval.

否则为您的脚本设置两个 crontab:

Otherwise set up two crontabs for your script:

0,40 */2 * * * /path/to/script/foo.sh
20 1,3,5,7,9,11,13,15,17,19,21,23 * * * /path/to/script/foo.sh

这篇关于如何设置 cron 每 40 分钟/25 分钟运行一次我的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆