在 Ubuntu 中,“定时器”通常有以下几种常见场景。我分别说明 如何启动。
sudo vim /etc/systemd/system/mytask.service
内容示例:
[Unit]
Description=My Task
[Service]
Type=simple
ExecStart=/usr/bin/echo "hello"
sudo vim /etc/systemd/system/mytask.timer
内容示例(每天 10:00 执行):
[Unit]
Description=Run My Task
[Timer]
OnCalendar=*-*-* 10:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now mytask.timer
systemctl status mytask.timer
systemctl list-timers
✅ 这就是“启动 Ubuntu 定时器”的标准方式
crontab -e
* * * * * /home/user/test.sh
sudo systemctl enable --now cron
查看状态:
systemctl status cron
sudo apt install at
sudo systemctl enable --now atd
echo "shutdown -h now" | at 23:00
| 方式 | 启动命令 |
|---|---|
| systemd timer | systemctl enable --now xxx.timer |
| cron | systemctl enable --now cron |
| at | systemctl enable --now atd |
如果你能告诉我 “你想定时做什么”,我可以帮你写完整配置。