在 Ubuntu 里,“trigger” 这个词可能对应好几种不同的东西,下面我先按最常见的几种场景给你说明,你看看你指的是哪一种,或直接告诉我你的具体需求。
这是 Ubuntu 上最标准的“trigger”方式。
示例:每 5 分钟执行一个脚本
sudo nano /etc/systemd/system/mytask.service
[Unit]
Description=My Task
[Service]
ExecStart=/usr/local/bin/myscript.sh
sudo nano /etc/systemd/system/mytask.timer
[Unit]
Description=Run mytask every 5 minutes
[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
[Install]
WantedBy=timers.target
sudo systemctl enable --now mytask.timer
查看:
systemctl list-timers
inotifywaitsudo apt install inotify-tools
示例:
inotifywait -m /var/www -e modify |
while read path action file; do
echo "$file changed"
/path/to/script.sh
done
常用于:
Ubuntu 的 apt 本身有 dpkg triggers
查看已有 trigger:
ls /var/lib/dpkg/triggers/
自定义 trigger(高级):
/etc/dpkg/triggers/
一般用户很少需要配置,除非你做 Debian 包开发。
crontab -e
示例(每天 3 点):
0 3 * * * /path/to/script.sh
那就不是 Ubuntu 系统配置,而是:
我可以给你精确到命令级的配置方案。