在 CentOS 系统中,定时任务通常是通过 cron 服务来管理和执行的。要启动、停止或重启 cron 服务,你可以使用以下命令:
cron 服务:sudo systemctl start crond.service
cron 服务:sudo systemctl stop crond.service
cron 服务:sudo systemctl restart crond.service
cron 服务开机自启:sudo systemctl enable crond.service
cron 服务开机自启:sudo systemctl disable crond.service
要编辑用户的定时任务,你可以使用 crontab 命令。以下是一些常用的 crontab 命令:
crontab -e
crontab -l
crontab -r
在 crontab 文件中,你可以按照以下格式添加定时任务:
* * * * * /path/to/command arg1 arg2
| | | | |
| | | | ----- 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)
每行代表一个定时任务,五个星号分别表示分钟、小时、日期、月份和星期。在这五个字段中,你可以使用 * 代表任意值,也可以使用逗号 , 分隔多个值,或者使用连字符 - 表示一个范围。