温馨提示×

debian定时器怎样排查故障

小樊
41
2026-10-01 21:23:29
栏目: 智能运维

在 Debian 里“定时器”通常有两种常见含义:

  1. systemd timer(现代 Debian 默认,用来替代 cron)
  2. cron / anacron

下面分别说明排查思路和常用命令。


一、systemd timer 排查

1. 查看定时器状态

systemctl list-timers

看目标 timer 是否:

  • 存在
  • 下次执行时间(LEFT/NEXT)
  • 是否 active

查看某个 timer:

systemctl status mytimer.timer

2. 查看定时器是否启用

systemctl is-enabled mytimer.timer
systemctl is-active mytimer.timer

若未启用:

systemctl enable --now mytimer.timer

3. 查看对应 service 是否失败

timer 只是“触发”,真正干活的是 .service:

systemctl status mytimer.service
journalctl -u mytimer.service

4. 手动执行测试

systemctl start mytimer.service

或手动触发 timer:

systemctl start mytimer.timer

5. 查看日志(非常关键)

journalctl -u mytimer.timer
journalctl -u mytimer.service
journalctl -xe

6. 常见错误点

  • .timer 里写的是 OnCalendar=* 格式错误
  • .service 里 Type=oneshot 没写
  • 脚本无执行权限
  • 用户 timer 放在 ~/.config/systemd/user/
  • 没执行 daemon-reload
systemctl daemon-reload

二、cron 排查

1. 服务是否运行

systemctl status cron

2. 查看 cron 日志

Debian 默认可能没开 cron 日志,在:

/var/log/syslog

搜索:

grep CRON /var/log/syslog

若没有,可启用 rsyslog 的 cron 日志。

3. 测试 cron 是否执行

在 crontab 里加:

* * * * * echo "cron ok" >> /tmp/cron_test.log

4. 常见坑

  • 环境变量不全(cron 环境很干净)
  • 脚本用相对路径
  • 没写绝对路径
  • 权限问题

三、快速排查 checklist(推荐顺序)

  1. systemctl list-timers
  2. systemctl status xxx.timer
  3. systemctl status xxx.service
  4. journalctl -u xxx
  5. 手动执行脚本本身
  6. 检查权限 / 路径 / 环境变量

如果你愿意,可以把:

  • 定时器类型(systemd / cron)
  • 相关 .timer / .service 文件
  • 报错日志

贴出来,我可以帮你逐行定位问题。

0 踩