在 Debian 系统中,我们通常说的“定时器”指的是 systemd 的 timer 单元(替代传统的 cron),也可能是 cron 服务。下面分别说明。
例如 /etc/systemd/system/mytask.service:
[Unit]
Description=My Task
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mytask.sh
例如 /etc/systemd/system/mytask.timer:
[Unit]
Description=Run My Task Daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now mytask.timer
systemctl status mytask.timer
systemctl list-timers
sudo apt update
sudo apt install cron
sudo systemctl enable --now cron
crontab -e
示例(每天 3 点执行):
0 3 * * * /usr/local/bin/mytask.sh
如果你说的是某一类具体“定时器”(如 at、anacron、嵌入式 watchdog),可以告诉我场景,我可以给更针对性的说明。