在 CentOS 系统中,使用 crontab
来设置定时任务。要设置执行间隔,你需要编辑当前用户的 crontab
文件。可以使用以下命令打开 crontab
文件:
crontab -e
接下来,你需要按照 cron 的时间格式来设置定时任务。Cron 的时间格式如下:
* * * * * command-to-be-executed - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday to Saturday; 7 is also Sunday) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)
要设置执行间隔,你可以使用以下方法:
- 使用
*/n
表示每隔 n 单位时间执行一次任务。例如,如果你想让任务每隔 5 分钟执行一次,可以这样设置:
*/5 * * * * command-to-be-executed
- 使用多个星号表示特定的时间点。例如,如果你想让任务在每天的 1:00、2:00 和 3:00 执行,可以这样设置:
0 1,2,3 * * * command-to-be-executed
- 使用逗号分隔多个值。例如,如果你想让任务在每周的周一、周三和周五执行,可以这样设置:
0 0 * * 1,3,5 command-to-be-executed
编辑完成后,保存并退出 crontab
文件。现在,你的定时任务将按照指定的执行间隔运行。