Debian 定时器修改时间间隔的两种常用方式
在 Debian 中,常见的定时器有两类:cron(传统分钟级调度)与 systemd timer(日历/单调时钟,支持更高精度与更复杂策略)。下面分别给出修改间隔的具体做法与示例。
使用 cron 调整间隔
crontab -e*/5 * * * * /path/to/script.sh0 * * * * /path/to/script.sh0 1 * * * /path/to/script.sh0 1 * * 0 /path/to/script.sh0 1 1 * * /path/to/script.shchmod +x /path/to/script.shsudo crontab -e(针对 root)使用 systemd timer 调整间隔
systemctl list-timers --allsudo nano /etc/systemd/system/mytask.timersudo systemctl daemon-reload && sudo systemctl restart mytask.timerOnCalendar=*-*-* *:*:00/1OnCalendar=*:0/5:00OnCalendar=*-*-* *:00:00OnCalendar=*-*-* 01:00:00OnCalendar=Sun *-*-* 01:00:00OnCalendar=*-*-01 01:00:00OnUnitActiveSec=1hOnUnitActiveSec=5minOnBootSec=5min 实现开机后延迟启动AccuracySec=1s(将触发精度收敛到 1 秒)RandomizedDelaySec=1m(在触发时间前后随机抖动最多 1 分钟,避免“惊群”)/etc/systemd/system/mytask.timer[Unit]
Description=Run mytask every 5 minutes
[Timer]
OnCalendar=*:0/5:00
Persistent=true
Unit=mytask.service
[Install]
WantedBy=timers.target
/etc/systemd/system/mytask.service[Unit]
Description=My scheduled task
[Service]
ExecStart=/path/to/script.sh
sudo systemctl enable --now mytask.timer如何选择与注意事项
* */5 * * *(后者含义不同)daemon-reload 并重启定时器