在Ubuntu中,可使用cron
或systemd
设置任务执行时间,具体如下:
crontab -e
编辑当前用户的crontab文件。分 时 日 月 星期 命令
添加定时任务,例如0 6 * * * /path/to/script.sh
表示每天早上6点执行脚本。/etc/systemd/system/mytask.service
,内容为[Unit] Description=My Task [Service] ExecStart=/path/to/script.sh
。/etc/systemd/system/mytask.timer
,内容为[Unit] Description=Run My Task [Timer] OnCalendar=*-*-* 06:00:00 Persistent=true [Install] WantedBy=timers.target
,其中OnCalendar
指定执行时间。sudo systemctl enable --now mytask.timer
。