在CentOS系统中,使用crontab来设置定时任务。以下是设置定时任务的步骤:
打开终端。
输入crontab -e命令来编辑当前用户的crontab文件。如果你想为其他用户设置定时任务,请使用sudo crontab -u username -e,其中username是要设置定时任务的用户。
在打开的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)
例如,如果你想要每天凌晨1点执行一个名为fetch_linux.sh的脚本,该脚本位于/home/user/scripts/目录下,你可以在crontab文件中添加以下行:
0 1 * * * /home/user/scripts/fetch_linux.sh
注意:确保你的脚本具有可执行权限。你可以使用chmod +x /home/user/scripts/fetch_linux.sh命令来添加可执行权限。
crontab -l命令。如果你想查看其他用户的定时任务,请使用sudo crontab -u username -l,其中username是要查看定时任务的用户。