在 Debian(以及其他使用 systemd 的 Linux 发行版)中,“定时器”通常指 systemd timer。下面从查看、调试、常见问题和示例几个方面说明如何调试 Debian 的定时器。
systemd 定时器由两部分组成:
.timer 文件:定义何时触发.service 文件:定义触发后执行什么例如:
mytask.timer
mytask.service
systemctl list-timers
查看某个定时器:
systemctl list-timers mytask.timer
systemctl status mytask.timer
systemctl status mytask.service
systemctl start mytask.service
如果服务本身失败,问题不在定时器。
systemctl start mytask.timer
systemctl trigger mytimer.timer
journalctl -u mytask.timer
journalctl -u mytask.service
journalctl -f -u mytask.service
journalctl -xe
检查:
systemctl is-enabled mytask.timer
如果不是 enabled,执行:
systemctl enable --now mytask.timer
查看系统时间:
timedatectl
如果时区不对:
timedatectl set-timezone Asia/Shanghai
示例(每天 3:00):
[Timer]
OnCalendar=*-*-* 03:00:00
检查语法:
systemd-analyze verify /etc/systemd/system/mytask.timer
检查:
systemctl status mytask.service
journalctl -u mytask.service
确保:
[Unit]
Description=Debug Timer
[Timer]
OnCalendar=*:*:00/10
Persistent=true
[Install]
WantedBy=timers.target
[Unit]
Description=Debug Service
[Service]
Type=oneshot
ExecStart=/usr/bin/echo "timer works" >> /tmp/timer.log
启用:
systemctl daemon-reload
systemctl enable --now debug.timer
systemctl daemon-reload
systemctl list-timers
systemctl status xxx.timer
journalctl -u xxx.service
systemd-analyze verify xxx.timer
如果你愿意,可以把你的 .timer 和 .service 文件内容发出来,我可以帮你具体指出问题在哪。