跳到主要内容

Crontab在11 月份内,每天的早上6 点到12 点中,每隔2 小时执行一次/usr/bin/httpd.sh 怎么实现?

参考答案:

要在Crontab中设置这样的定时任务,首先我们要理解Cron的语法。Cron的语法如下:

* * * * * command-to-be-executed
- - - - -
| | | | | 
| | | | +---- Day of the week (0 - 7) [Both 0 and 7 represent Sunday]
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)

对于您的需求,我们需要在11月份,每天的早上6点到12点中,每隔2小时执行一次/usr/bin/httpd.sh。我们可以将这个时间段拆分为6点、8点、10点和12点四个时间点来分别设置。

这里是一个示例的Crontab配置:

# 在11月的每天6点执行
0 6 * 11 * /usr/bin/httpd.sh

# 在11月的每天8点执行
0 8 * 11 * /usr/bin/httpd.sh

# 在11月的每天10点执行
0 10 * 11 * /usr/bin/httpd.sh

# 在11月的每天12点执行
0 12 * 11 * /usr/bin/httpd.sh

这四条Crontab规则分别对应了11月份每天的6点、8点、10点和12点执行/usr/bin/httpd.sh这个脚本。

需要注意的是,这种方法的一个限制是,它只能在指定的时间点执行,而不能直接在指定的时间范围内每隔一定的时间间隔执行。如果你需要更复杂的定时任务,可能需要考虑使用其他工具,如anacronsystemd timers

另外,请确保/usr/bin/httpd.sh这个脚本具有执行权限,否则Cron无法执行它。你可以使用chmod +x /usr/bin/httpd.sh来添加执行权限。