在Ubuntu系统中,自定义触发器规则可通过系统日志工具、定时任务调度、文件系统监控、系统服务管理及专用工具等多种方式实现,覆盖时间触发、事件触发等多种场景。以下是具体方法及示例:
Systemd是Ubuntu的核心服务管理工具,可通过服务单元文件定义触发条件,触发器文件关联特定事件(如服务启动/停止)。
/etc/systemd/system/my_custom_trigger.service),定义服务行为:[Unit]
Description=Custom Trigger Service (Runs on specific events)
[Service]
Type=oneshot # 仅执行一次
ExecStart=/path/to/your/script.sh # 触发后执行的脚本
[Install]
WantedBy=multi-user.target # 服务所属目标(多用户模式)
/etc/systemd/system/my_custom_trigger.trigger),关联事件(如start操作):[Trigger]
Operation=start # 触发事件(如服务启动)
ExecStart=/path/to/your/script.sh # 触发时执行的脚本
sudo systemctl enable my_custom_trigger.trigger # 启用触发器
sudo systemctl start my_custom_trigger.trigger # 启动触发器
此方式适用于系统服务事件(如某服务启动时触发脚本)。Cron是Ubuntu默认的时间基任务调度工具,适合按固定周期(如每分钟、每天凌晨)触发任务。
crontab -e
分钟 小时 日 月 周 命令,例如:
0 2 * * * /path/to/your/script.sh*/5 * * * * /path/to/your/script.sh@reboot /path/to/your/script.shInotify是Linux内核的文件系统监控工具,可实时响应文件/目录的创建、修改、删除等事件。
sudo apt install inotify-tools
monitor.sh):#!/bin/bash
WATCH_DIR="/path/to/monitor" # 监控的目录
TRIGGER_STRING="keyword" # 触发条件(文件名包含的关键字)
LOG_FILE="/var/log/inotify.log" # 日志文件
inotifywait -m -r -e create,modify --format '%w%f' "$WATCH_DIR" | while read file; do
if [[ "$file" == *"$TRIGGER_STRING"* ]]; then
echo "$(date): File $file triggered!" >> "$LOG_FILE"
/path/to/your/script.sh "$file" # 触发后执行的脚本
fi
done
chmod +x monitor.sh
./monitor.sh # 后台运行:nohup ./monitor.sh &
此方式适用于文件系统实时事件(如上传文件到指定目录时触发备份)。Ubuntu Trigger是专门用于自动化任务调度的工具,支持通过YAML配置文件定义复杂的触发规则(如系统运行时间、文件修改、邮件通知)。
sudo apt update && sudo apt install ubuntu-trigger
example-trigger.yaml):trigger:
name: "Uptime Check Trigger" # 触发器名称
description: "Triggers when uptime exceeds 1 day"
rules:
- id: "rule1"
description: "Check system uptime"
condition:
type: "uptime" # 条件类型:系统运行时间
threshold: 86400 # 阈值(1天=86400秒)
action:
type: "shell" # 动作类型:执行shell命令
command: "echo 'System uptime exceeded 1 day!' | mail -s 'Alert' your-email@example.com"
- id: "rule2"
description: "Monitor file modification"
condition:
type: "file" # 条件类型:文件事件
path: "/path/to/important_file.conf" # 监控的文件路径
event: "modified" # 事件类型:修改
action:
type: "log" # 动作类型:记录日志
log_file: "/var/log/file_monitor.log"
triggertool create --config example-trigger.yaml
此方式适合复杂规则(如结合系统状态、文件变化、通知等多条件触发)。若需要高度定制化(如结合API调用、数据库查询),可使用Python编写触发器脚本,通过循环检查条件或系统调用实现。
custom_trigger.py):import os
import time
from datetime import datetime
LOG_FILE = "/var/log/python_trigger.log"
WATCH_FILE = "/path/to/watched_file.txt"
def check_condition():
"""自定义触发条件(如文件最后修改时间超过1小时)"""
if os.path.exists(WATCH_FILE):
mod_time = os.path.getmtime(WATCH_FILE)
current_time = time.time()
if current_time - mod_time > 3600: # 1小时=3600秒
return True
return False
def execute_action():
"""触发后执行的动作(如记录日志、发送通知)"""
with open(LOG_FILE, "a") as f:
f.write(f"[{datetime.now()}] Condition met. Executing action...\n")
# 示例:发送邮件(需安装sendmail或使用第三方库)
# os.system('echo "Condition met" | mail -s "Alert" your-email@example.com')
if __name__ == "__main__":
while True:
if check_condition():
execute_action()
time.sleep(60) # 每分钟检查一次
chmod +x custom_trigger.py
nohup python3 custom_trigger.py & # 后台运行
此方式适合复杂业务逻辑(如结合外部API、数据库操作)。chmod +x),必要时使用sudo提升权限。/var/log/trigger.log),便于排查问题。通过上述方法,可根据具体需求(时间、事件、系统状态)自定义Ubuntu触发器规则,实现自动化任务调度与事件响应。