你提到的 “Ubuntu Trigger” 在 Ubuntu 官方文档里并不是一个固定、单一的工具名称,所以要先确认你指的是哪一类“触发器”。常见有以下几种情况,我分别说明用法,你可以对号入座。
这是 Ubuntu 上最常用、最标准的“trigger”方式,用来替代 cron。
sudo nano /etc/systemd/system/mytask.service
内容示例:
[Unit]
Description=My Task
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mytask.sh
sudo nano /etc/systemd/system/mytask.timer
示例(每天 8 点触发):
[Unit]
Description=Run mytask daily
[Timer]
OnCalendar=*-*-* 08:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now mytask.timer
systemctl list-timers
✅ 适合场景:开机触发、定时触发、延迟触发
用于“当某个文件或目录变化时触发任务”。
sudo apt install inotify-tools
示例:
inotifywait -m /data -e create -e modify |
while read path action file; do
echo "$file changed"
done
✅ 适合场景:自动备份、自动编译、文件监控
比如 Git 提交后自动触发部署。
nc -l 9000 | while read line; do
/opt/deploy.sh
done
或使用 GitHub Webhooks + nginx + 脚本
✅ 适合场景:CI/CD、自动部署
例如:
这些不是 Ubuntu 特有,而是应用层触发器。
| 你想做的事 | 推荐方式 |
|---|---|
| 定时触发 | systemd timer ✅ |
| 文件变化触发 | inotify / fswatch |
| 网络事件触发 | webhook |
| 数据库触发 | SQL Trigger |
| 开机触发 | systemd service |
你可以直接告诉我:
我可以给你精确到命令级别的完整示例。