目标
在Linux服务器每次启动一段时间后执行特定任务。
适用范围
适用于使用 systemd 的系统(如 Ubuntu 16.04+、CentOS 7+)
操作方式
1. 创建服务单元文件
sudo vim /etc/systemd/system/after-boot.service
写入以下内容:
[Unit]
Description=Execute script 5 minutes after boot
[Service]
Type=oneshot
ExecStart=/path/to/your_script.sh # 替换为你的脚本路径
保存退出。
2. 创建计时器单元文件
sudo vim /etc/systemd/system/after-boot.timer
写入以下内容:
[Unit]
Description=Run service 5 minutes after boot
[Timer]
OnBootSec=5min
Unit=after-boot.service
[Install]
WantedBy=timers.target
3. 启用并启动计时器
sudo systemctl daemon-reload
sudo systemctl enable --now after-boot.timer
验证状态:systemctl status after-boot.timer
在 Linux 中手动触发 Systemd Timer 的指南
方法一:直接启动关联的 Service(推荐)
### 1. 找到与 Timer 关联的 Service 名称
sudo systemctl list-timers | grep your-timer-name
输出示例:
after-boot.timer loaded active waiting Run service 5 minutes after boot
手动启动关联的 Service:
sudo systemctl start after-boot.service # 替换为您的 Service 名称
方法二:重置 Timer 并强制立即运行
1. 停止 Timer
sudo systemctl stop after-boot.timer
2. 重置 Timer 的最后触发时间
sudo systemctl reset-failed after-boot.timer
3. 启动 Timer 并强制立即触发
sudo systemctl start after-boot.timer
方法三:使用 systemd-run 临时执行
特殊情况下,可以直接运行命令:
sudo systemd-run --unit=manual-trigger.service /path/to/your_script.sh
🔍 验证执行结果
查看服务日志确认是否成功执行:
# 查看 Service 日志
sudo journalctl -u after-boot.service -b --since "1 hour ago"
# 查看所有日志
sudo journalctl -f
⚠ 重要注意事项
避免冲突:手动触发不会影响定时器的正常调度
权限要求:所有操作都需要root
或sudo
权限
环境差异:手动触发可能使用不同环境变量(使用systemctl show
检查)
服务类型:确保 Service 文件中的Type=
设置正确
对于简单脚本:Type=oneshot
对于持续服务:Type=simple