温馨提示×

为什么debian定时器失败

小樊
38
2026-09-28 23:56:20
栏目: 智能运维

Debian 里“定时器失败”通常指 systemd timer 没有按预期触发,或者 cron / at 任务没执行。下面按常见原因系统说明,你可以对照排查。


一、如果是 systemd timer(*.timer)

1. timer 没启用

systemctl list-timers
systemctl status yourtimer.timer

如果没启用:

systemctl enable --now yourtimer.timer

2. 对应的 .service 失败

timer 只负责“触发”,真正执行的是 .service:

systemctl status yourservice.service
journalctl -u yourservice.service

常见错误:

  • ExecStart 路径错误
  • 权限不足
  • 用户不存在

3. OnCalendar 写错

示例:

OnCalendar=*-*-* 02:00:00

错误写法会导致 timer 永不触发。

检查:

systemd-analyze calendar "你的时间表达式"

4. 系统时间不对

timedatectl

NTP 未同步会导致 timer 行为异常:

timedatectl set-ntp true

5. 系统休眠 / 关机

timer 在关机期间不会补执行(除非 Persistent=true):

[Timer]
Persistent=true

二、如果是 cron(/etc/cron.* 或 crontab)

1. cron 服务没运行

systemctl status cron

没运行就:

systemctl enable --now cron

2. 环境变量问题

cron 环境非常干净,常见失败:

  • PATH 不存在
  • 脚本用了 ~
  • 没有输出重定向

✅ 建议写法:

0 2 * * * /usr/bin/python3 /opt/script.py >> /var/log/script.log 2>&1

3. 权限问题

  • /etc/cron.d/ 文件权限必须是 644
  • 用户不在 cron.allow

三、如果是 at

systemctl status atd
atq

atd 没启动会导致任务不执行。


四、快速排查清单 ✅

journalctl -xe
systemctl list-timers --all
systemctl status cron
date

如果你能贴出:

  • 用的是 timer 还是 cron
  • 具体配置文件
  • 报错日志

我可以直接帮你定位是哪一步错了。

0 踩