在Ubuntu虚拟机中设置定时任务,通常使用cron
这个命令。以下是设置定时任务的步骤:
打开终端(快捷键:Ctrl + Alt + T)。
输入以下命令,以编辑当前用户的crontab文件:
crontab -e
如果你想编辑其他用户的crontab文件,请使用以下命令(将username
替换为目标用户名):
sudo crontab -u username -e
* * * * * 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/your_username/scripts/
目录下),你可以添加以下行:
0 6 * * * /home/your_username/scripts/backup.sh
注意:确保你的脚本具有可执行权限。如果没有,可以使用以下命令添加可执行权限:
chmod +x /home/your_username/scripts/backup.sh
crontab -l
如果你想查看其他用户的定时任务列表,请使用以下命令(将username
替换为目标用户名):
sudo crontab -u username -l