背景&需求
需要频繁更改一个服务的配置文件进行测试
实现
配置服务的systemd文件
vim /lib/systemd/system/xxx.service
[Unit]
Description=xxx daemon, A rule-based proxy in Go.
[Service]
Type=simple
ExecStart=/opt/xxx/xxx-d /etc/xxx/
Restart=always
[Install]
WantedBy=multi-user.target
配置path服务
用来监听服务配置文件是否有变化
vim /lib/systemd/system/xxx-restart.path
[Unit]
Description="monitor xxx configureation"
After=network-online.target
[Path]
Unit=xxx-restart.service
PathChanged=/etc/xxx/config.yaml
[Install]
WantedBy=multi-user.target
配置执行重启的服务
vim /lib/systemd/system/xxx-restart.service
[Unit]
Description=update xxx config,restart xxx service
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart xxx.service
[Install]
WantedBy=multi-user.target
启动服务
systemctl daemon-reload
systemctl enable --now xxx.service
systemctl enable --now xxx-restart.path
systemctl enable --now xxx-restart.service
验证
随便更改监听的配置文件的内容
systemd服务会自动重启
refer
https://blog.csdn.net/sanhewuyang/article/details/127522317