在 Debian 里“定时器开机启动”通常分两种场景,最常见的是 systemd 定时器(timer),另一种是比较老的 cron @reboot。下面分别说。
假设你有一个服务:
/etc/systemd/system/hello.service
[Unit]
Description=Hello
[Service]
ExecStart=/usr/bin/echo hello
和一个定时器:
/etc/systemd/system/hello.timer
[Unit]
Description=Run hello
[Timer]
OnBootSec=1min
OnUnitActiveSec=1h
[Install]
WantedBy=timers.target
sudo systemctl enable hello.timer
✅
enable就是“开机启动”
❌ 只start不会开机自启
sudo systemctl start hello.timer # 立即启动
systemctl status hello.timer # 查看状态
systemctl list-timers # 查看所有定时器
journalctl -u hello.service # 看执行日志
编辑 crontab:
crontab -e
添加:
@reboot /path/to/script.sh
⚠ 注意:
[Timer]
OnBootSec=30s
表示开机 30 秒后执行一次。
enableservice 而不是 timer如果你愿意,可以把你的 timer 文件或需求贴出来,我可以帮你直接改好。