Debian 下“定时器失效”一般不是单一原因,常见情况如下(按出现频率排序):
最常见原因。
systemctl list-timers
systemctl status your.timer
如果看不到你的 timer,说明没启用:
systemctl enable --now your.timer
⚠️ 只 enable 不 --now,或只启动 .service 而不启动 .timer,都不会触发。
OnCalendar 写错会导致 timer 根本不触发。
错误示例:
OnCalendar=every day 3:00
正确示例:
OnCalendar=*-*-* 03:00:00
修改后必须:
systemctl daemon-reload
systemctl restart your.timer
systemd timer 默认不会补执行:
解决:
Persistent=true
放在 [Timer] 段中。
Debian 默认可能 没装 cron:
systemctl status cron
若未运行:
apt install cron
systemctl enable --now cron
检查用户 cron:
crontab -l
⚠️ 注意:
很多“定时器失效”其实是脚本没跑起来:
~/xxxpython 但 PATH 不对建议:
[Service]
Environment=PATH=/usr/bin:/bin
或在脚本里:
#!/bin/bash
exec >> /var/log/mytask.log 2>&1
时间错了,timer / cron 都不会按预期触发。
timedatectl
若不对:
apt install ntpsec
systemctl enable --now ntpsec
用户 timer 需要用户会话:
systemctl --user list-timers
若未启用:
systemctl --user enable --now mytimer.timer
且用户需 loginctl enable-linger 用户名
systemctl list-timers
systemctl status your.timer
journalctl -u your.service
journalctl -u your.timer
如果你愿意,可以把:
systemctl status 输出贴出来,我可以直接帮你定位是哪一条。