在 Ubuntu(或一般 Linux)里,“trigger” 这个词可能指几种不同东西,下面我按常见场景给你说明,你看看你指的是哪一种。
如果你是想在某个事件发生时自动执行任务(比如开机、网络就绪、U 盘插入等),用的是 systemd。
sudo nano /etc/systemd/system/hello.service
内容示例:
[Unit]
Description=Hello Trigger
[Service]
Type=oneshot
ExecStart=/usr/bin/echo "Triggered!"
sudo nano /etc/systemd/system/watch.path
[Unit]
Description=Watch file
[Path]
PathModified=/tmp/test.txt
[Install]
WantedBy=multi-user.target
启用:
sudo systemctl enable --now watch.path
当 /tmp/test.txt 被修改时,hello.service 会被触发。
如果你是指 dpkg / apt 的 trigger:
# 查看已注册的 trigger
dpkg-trigger --help
在打包时定义:
# debian/triggers
interest /usr/share/mylib
通常不推荐普通用户使用。
如果是定时任务:
crontab -e
示例(每天 3 点):
0 3 * * * /path/script.sh
U 盘、网卡等硬件触发:
sudo nano /etc/udev/rules.d/99-mytrigger.rules
示例:
ACTION=="add", SUBSYSTEM=="block", RUN+="/usr/local/bin/usb.sh"
你可以告诉我:
我可以直接给你完整可用的配置示例。