在 Debian 里“定时器”通常指 systemd timer(推荐方式),也可以用传统的 cron。下面分别说明。
例如每天执行 /opt/backup.sh
# /etc/systemd/system/backup.service
[Unit]
Description=Backup Script
[Service]
Type=oneshot
ExecStart=/opt/backup.sh
# /etc/systemd/system/backup.timer
[Unit]
Description=Run backup daily
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers
systemctl status backup.timer
OnCalendar=*-*-* 03:00:00OnCalendar=hourlyOnCalendar=*:0/10OnCalendar=Mon *-*-* 05:00:00crontab -e
0 2 * * * /opt/backup.sh
含义:
分 时 日 月 周
0 2 * * * → 每天 2:00
crontab -l
/etc/cron.d/
/etc/cron.daily/
/etc/cron.hourly/
如果你说明具体要多久执行一次、做什么,我可以直接帮你写完整配置。