温馨提示×

debian定时器如何排查

小樊
47
2026-10-10 17:41:23
栏目: 智能运维

在 Debian(以及大多数 Linux 系统)里,“定时器”通常分 两类:

  1. systemd 定时器(systemd timer)
  2. cron / anacron 定时任务

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


一、systemd 定时器排查

1. 查看所有定时器

systemctl list-timers

或只看失败的:

systemctl list-timers --failed

2. 查看某个定时器状态

systemctl status mytimer.timer

3. 查看定时器配置

systemctl cat mytimer.timer

重点看:

  • OnCalendar=
  • Persistent=
  • Unit=(绑定的 service)

4. 查看对应的 service 是否执行失败

systemctl status mytimer.service
journalctl -u mytimer.service

5. 查看日志

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

6. 手动测试

sudo systemctl start mytimer.service

7. 常见坑

  • timer 没 enabled
    systemctl enable mytimer.timer
    
  • 时间写错(时区问题)
  • service 里路径用的是相对路径
  • User= 权限不足

二、cron 定时任务排查

1. 查看当前用户的 cron

crontab -l

2. 查看系统级 cron

ls -l /etc/cron.*
cat /etc/crontab

3. 查看 cron 服务状态

systemctl status cron

4. 查看 cron 日志(Debian)

journalctl -u cron

或

grep CRON /var/log/syslog

5. 手动执行测试

/bin/bash /path/to/script.sh

6. 常见坑

  • 环境变量不全(cron 环境极小)
  • 脚本没加可执行权限
  • 路径写成相对路径
  • 输出没重定向,导致静默失败

建议:

* * * * * /bin/bash /path/script.sh >> /var/log/script.log 2>&1

三、快速定位思路(通用)

  1. 先确认定时器是否触发
    • list-timers 或 cron 日志
  2. 再确认任务是否执行
    • service 状态 / 脚本日志
  3. 最后确认执行结果
    • 权限、环境变量、路径

如果你能贴出:

  • 用的是 systemd timer 还是 cron
  • 相关 配置文件内容
  • 或 报错日志

我可以直接帮你精确定位问题。

0 踩