在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
以上就是在Ubuntu虚拟机中设置定时任务的方法。