在 Debian(以及大多数 Linux 发行版)中,“定时器”通常指 systemd 定时器(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
[Unit]
Description=Run My Task
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
[Install]
WantedBy=timers.target
OnCalendar=*-*-* 02:30:00
OnCalendar=hourly
OnCalendar=*-*-* *:*:00
OnCalendar=Mon *-*-* 03:00:00
systemctl daemon-reexec
systemctl enable --now mytask.timer
systemctl list-timers
systemctl status mytask.timer
crontab -e
分 时 日 月 周 命令
示例:
30 2 * * * /usr/local/bin/mytask.sh
*/5 * * * * /usr/local/bin/mytask.sh
0 3 * * 1 /usr/local/bin/mytask.sh
crontab -l
日志通常在:
journalctl -u cron
# 或
/var/log/syslog
如果你能说下是 系统服务定时 还是 用户脚本定时,我可以给你更具体的示例。