Ubuntu配置定时器常用cron
和systemd timer
两种方式,具体如下:
cron
服务已安装并启动:sudo apt install cron
,sudo systemctl start cron
,sudo systemctl enable cron
。crontab
文件:crontab -e
。0 8 * * * /path/to/script.sh
(每天8点执行脚本)。crontab -l
查看任务列表。sudo nano /etc/systemd/system/mytask.service
,内容为[Unit] Description=My Task [Service] ExecStart=/path/to/script.sh
。sudo nano /etc/systemd/system/mytask.timer
,内容为[Unit] Description=Run My Task [Timer] OnCalendar=*-*-* 08:00:00 Persistent=true [Install] WantedBy=timers.target
。systemd
配置:sudo systemctl daemon-reload
。sudo systemctl enable --now mytask.timer
。