在CentOS中,你可以使用cron
服务来设置定时任务。以下是设置定时任务的步骤:
打开终端。
输入crontab -e
命令来编辑当前用户的crontab文件。如果你想为其他用户设置定时任务,请使用sudo crontab -u username -e
命令。
在打开的crontab文件中,你可以按照以下格式添加定时任务:
* * * * * 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)
例如,如果你想每天早上6点执行一个名为backup.sh
的脚本,该脚本位于/home/user/scripts/
目录下,你可以添加以下行:
0 6 * * * /home/user/scripts/backup.sh
保存并关闭crontab文件。
要查看当前用户的定时任务列表,请运行crontab -l
命令。如果你想查看其他用户的定时任务,请使用sudo crontab -u username -l
命令。
确保cron
服务正在运行。你可以使用以下命令来启动cron
服务:
sudo systemctl start crond
cron
服务在系统启动时自动运行,请执行以下命令:sudo systemctl enable crond
现在,你已经成功设置了CentOS中的定时任务。