在 Debian(以及大多数 Linux 发行版)中,**定时器(timer)**通常是指 systemd 定时器,而不是 cron。下面分情况说明 如何启动/启用/触发 systemd 定时器。
systemctl list-timers
或查看所有(包括未激活的):
systemctl list-timers --all
sudo systemctl start yourtimer.timer
例如:
sudo systemctl start backup.timer
sudo systemctl enable yourtimer.timer
启动并设置自启:
sudo systemctl enable --now yourtimer.timer
systemctl status yourtimer.timer
查看下次触发时间:
systemctl list-timers
sudo systemctl start yourtimer.service
⚠️ 注意:
.timer 负责定时.service 才是真正执行任务systemctl list-timers
例如:
NEXT LEFT LAST PASSED UNIT ACTIVATES
Mon 2026-01-19 00:00:00 CST 5h left Sun 2026-01-18 00:00:00 CST 18h ago apt-daily.timer apt-daily.service
sudo systemctl disable yourtimer.timer
sudo systemctl stop yourtimer.timer
如果是 cron:
sudo systemctl start cron
sudo systemctl enable cron
编辑定时任务:
crontab -e
| 操作 | 命令 |
|---|---|
| 启动定时器 | systemctl start xxx.timer |
| 开机自启 | systemctl enable xxx.timer |
| 启动并自启 | systemctl enable --now xxx.timer |
| 查看状态 | systemctl status xxx.timer |
| 手动执行 | systemctl start xxx.service |
如果你愿意,可以告诉我:
我可以帮你写完整的 .timer + .service 示例 ✅