在 CentOS 系统中,你可以使用 cron
服务来设置定时任务。以下是设置定时任务的步骤:
打开终端。
输入以下命令以编辑当前用户的 crontab 文件:
crontab -e
如果你需要使用 root 用户权限编辑 crontab 文件,请使用以下命令:
sudo crontab -e
* * * * * /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)
例如,如果你想要每天早上 6 点执行一个名为 backup.sh
的脚本,该脚本位于 /home/user/backup.sh
,你可以这样设置:
0 6 * * * /home/user/backup.sh
保存并关闭 crontab 文件。
要查看当前用户的定时任务列表,请运行以下命令:
crontab -l
如果你需要使用 root 用户权限查看定时任务列表,请使用以下命令:
sudo crontab -l
现在,你已经成功设置了 CentOS 系统中的定时任务。请确保你的脚本具有可执行权限,可以使用以下命令为脚本添加可执行权限:
chmod +x /path/to/script