温馨提示×

ubuntu触发器使用技巧有哪些

小樊
45
2026-01-11 16:57:42
栏目: 智能运维

Ubuntu 触发器使用技巧

一 核心思路与选型

  • 时间触发:用 cron 做简单周期任务;用 systemd Timer 做更灵活的日历/间隔调度、日志与依赖管理。
  • 事件触发:用 inotify 监听目录/文件的创建、修改、删除等变化;用 udev 响应 USB/块设备 插拔;用 systemd Path 监听文件/目录或套接字等路径变化。
  • 系统内置触发器:如 apt-daily.timer/apt-daily-upgrade.timer 每天触发更新检查与安装,可据此调整或关闭自动升级行为。

二 时间触发技巧

  • cron 快速上手
    • 编辑任务:crontab -e
    • 示例:每 5 分钟执行并追加日志
      */5 * * * * /path/to/script.sh >> /var/log/script.log 2>&1
      
    • 适合固定频率、简单脚本;注意环境变量与路径问题,尽量使用绝对路径。
  • systemd Timer 更可控
    • 服务单元:myjob.service
      [Unit]
      Description=My triggered job
      
      [Service]
      Type=oneshot
      ExecStart=/usr/local/bin/myjob.sh
      
    • 定时器单元:myjob.timer
      [Unit]
      Description=Run myjob every 5 minutes
      
      [Timer]
      OnBootSec=5min
      OnUnitActiveSec=5min
      Persistent=true
      
      [Install]
      WantedBy=timers.target
      
    • 启用:sudo systemctl daemon-reload && sudo systemctl enable --now myjob.timer
    • 查看:systemctl list-timers
    • 适合需要日志、依赖、开机延迟、持久化错过执行的场景。

三 事件触发技巧

  • inotify 实时监听目录
    • 安装:sudo apt-get install inotify-tools
    • 监听并触发脚本(防抖示例)
      #!/usr/bin/env bash
      WATCH=/data/incoming
      SCRIPT=/usr/local/bin/handle.sh
      inotifywait -m -r -e create,modify,close_write --format '%w%f %e' "$WATCH" | \
        while IFS=' ' read -r file event; do
          # 简单防抖:同一文件 2 秒内只处理一次
          if [[ -z "${last[$file]}" || $((SECONDS - last[$file])) -ge 2 ]]; then
            "$SCRIPT" "$file" "$event" &
            last["$file"]=$SECONDS
          fi
        done
      
    • 适合日志落地、文件投递、自动导入等实时场景。
  • udev 设备插拔触发
    • 规则示例(按厂商/产品 ID 触发)
      # /etc/udev/rules.d/99-usb-backup.rules
      ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="1234", ATTR{idProduct}=="5678", \
        RUN+="/usr/local/bin/usb-backup.sh %k"
      
    • 生效:sudo udevadm control --reload-rules && sudo udevadm trigger
    • 适合外置硬盘、扫码枪、摄像头等硬件事件。
  • systemd Path 监听路径变化
    • 服务:/etc/systemd/system/filewatcher.service
      [Unit]
      Description=Watch a file
      
      [Service]
      Type=oneshot
      ExecStart=/usr/local/bin/onfilechange.sh
      
    • 路径单元:/etc/systemd/system/filewatcher.path
      [Path]
      PathModified=/data/trigger.txt
      [Install]
      WantedBy=multi-user.target
      
    • 启用:sudo systemctl enable --now filewatcher.path
    • 适合无需常驻脚本、由 systemd 托管路径事件的场景。

四 实战与排错建议

  • 日志与输出
    • 将脚本输出重定向到日志文件(如 >> /var/log/... 2>&1),并在 systemd 单元中使用 StandardOutput=journaljournalctl -u <unit> 查看。
  • 幂等与防抖
    • 对 inotify 事件做去重/节流(如基于文件与时间的简单缓存),避免短时间重复处理同一文件。
  • 避免并发
    • 在脚本开头使用文件锁(如 flock -n)或 systemd 的 ExecStartPre/ExecStartPost 控制并发。
  • 权限与路径
    • 使用 绝对路径;确保执行用户对相关目录/设备有合适权限;必要时在 udev 规则中使用 RUN{program} 提升权限执行。
  • 避免与系统定时器冲突
    • 若需要关闭系统自动更新,可停止并禁用相关定时器与任务:
      sudo systemctl stop apt-daily.timer apt-daily-upgrade.timer
      sudo systemctl disable apt-daily.timer apt-daily-upgrade.timer
      sudo systemctl stop apt-daily.service apt-daily-upgrade.service
      sudo systemctl disable apt-daily.service apt-daily-upgrade.service
      
    • 注意:关闭自动升级后仍需定期手动更新。

0