在 Debian 中,“定时器周期”通常指 systemd timer(推荐方式)或 cron。下面分别说明。
例如 /etc/systemd/system/mytask.service:
[Unit]
Description=My Task
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mytask.sh
例如 /etc/systemd/system/mytask.timer:
固定周期(如每 10 分钟)
[Unit]
Description=Run mytask every 10 minutes
[Timer]
OnCalendar=*:0/10
Persistent=true
[Install]
WantedBy=timers.target
每天凌晨 3 点
OnCalendar=03:00
每小时
OnCalendar=hourly
每周一 4 点
OnCalendar=Mon 04:00
systemctl daemon-reload
systemctl enable --now mytask.timer
systemctl list-timers
systemctl status mytask.timer
编辑当前用户 cron:
crontab -e
示例:
# 每 10 分钟
*/10 * * * * /usr/local/bin/mytask.sh
# 每天 3 点
0 3 * * * /usr/local/bin/mytask.sh
# 每周一 4 点
0 4 * * 1 /usr/local/bin/mytask.sh
系统级 cron:
/etc/cron.d/
/etc/cron.daily/
/etc/cron.hourly/
| 需求 | systemd | cron |
|---|---|---|
| 每 5 分钟 | OnCalendar=*:0/5 |
*/5 * * * * |
| 每小时 | OnCalendar=hourly |
0 * * * * |
| 每天 | OnCalendar=daily |
0 0 * * * |
| 每周 | OnCalendar=weekly |
0 0 * * 0 |
如果你有 具体周期需求(如“每 2 小时”“工作日 9–18 点”),可以告诉我,我帮你写完整配置。