在 Debian 里“定时器”通常有两种常见含义,我分别说明,你可按需选择。
Debian 8 及以后默认使用 systemd,定时任务可通过 .timer 单元实现。
systemctl list-timers
定时器由两部分组成:
xxx.service:真正执行的任务xxx.timer:定时规则编辑定时器示例:
sudo systemctl edit --full mytimer.timer
常见时间配置:
[Timer]
OnCalendar=*-*-* 03:00:00 # 每天 3 点
Persistent=true
修改后重载并重启:
sudo systemctl daemon-reload
sudo systemctl restart mytimer.timer
crontab -e
示例(每天 2 点执行):
0 2 * * * /path/to/script.sh
sudo nano /etc/crontab
或放入:
/etc/cron.d/
crontab -l # 查看
crontab -r # 删除
若是 systemd 服务延迟启动:
[Service]
ExecStartPre=/bin/sleep 30
如果你能补充说明:
我可以给你更精确的修改示例。