温馨提示×

怎样配置ubuntu trigger

小樊
54
2026-09-09 15:55:54
栏目: 智能运维

在 Ubuntu 里,“trigger” 这个词可能对应好几种不同的东西,下面我先按最常见的几种场景给你说明,你看看你指的是哪一种,或直接告诉我你的具体需求。


一、如果你是问 systemd trigger(定时器 / 触发)

1️⃣ 使用 systemd timer(推荐)

这是 Ubuntu 上最标准的“trigger”方式。

示例:每 5 分钟执行一个脚本

创建 service

sudo nano /etc/systemd/system/mytask.service
[Unit]
Description=My Task

[Service]
ExecStart=/usr/local/bin/myscript.sh

创建 timer

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

二、如果你是问 inotify / 文件变化 trigger(如文件改动触发脚本)

使用 inotifywait

sudo apt install inotify-tools

示例:

inotifywait -m /var/www -e modify |
while read path action file; do
    echo "$file changed"
    /path/to/script.sh
done

常用于:

  • 自动备份
  • 热重载配置
  • 同步文件

三、如果你是问 apt / dpkg trigger(软件包触发器)

Ubuntu 的 apt 本身有 dpkg triggers

查看已有 trigger:

ls /var/lib/dpkg/triggers/

自定义 trigger(高级):

/etc/dpkg/triggers/

一般用户很少需要配置,除非你做 Debian 包开发。


四、如果你是问 cron(定时触发)

crontab -e

示例(每天 3 点):

0 3 * * * /path/to/script.sh

五、如果你是问 CI / Git trigger(如 GitHub Actions、Jenkins)

那就不是 Ubuntu 系统配置,而是:

  • webhook
  • push event
  • schedule

✅ 你可以直接告诉我:

  1. 你想“触发”的是什么?(脚本 / 服务 / 文件变化 / 定时任务)
  2. Ubuntu 版本(20.04 / 22.04 / 24.04)
  3. 手动执行还是自动执行?

我可以给你精确到命令级的配置方案。

0 踩