在 Ubuntu 里“定时器”通常有两种常见理解,我先分别说明,你按需选择:
Ubuntu 默认使用 systemd,它自带定时功能,比 cron 更强大。
sudo nano /etc/systemd/system/mytimer.service
内容示例:
[Unit]
Description=My Timer Task
[Service]
Type=oneshot
ExecStart=/usr/bin/echo "hello"
sudo nano /etc/systemd/system/mytimer.timer
内容示例(每天 3 点执行):
[Unit]
Description=Run mytask daily
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now mytimer.timer
systemctl list-timers
Ubuntu 一般已安装:
sudo apt update
sudo apt install cron
sudo systemctl enable --now cron
crontab -e
示例(每天 3 点执行脚本):
0 3 * * * /home/user/script.sh
如果你是 Ubuntu 桌面版:
gnome-schedule(旧版)如果你能告诉我:
我可以给你更精确的示例。