文件更改时如何重新启动 Systemd 服务。我有一个 Java 服务,我想在任何 jar 文件更改时重新加载它。
这是我的设置:
服务端
[Unit]
Description=srv 0.1: Service's description
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/srv
ExecStart=/opt/srv/bin/srv
User=root
Group=root
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
服务路径
[Path]
PathModified=/opt/srv/lib/
Run Code Online (Sandbox Code Playgroud)
我还尝试使用PathChanged
文件而不是目录。
当我安装我执行的服务时:sudo systemctl daemon-reload
和 sudo systemctl enable srv
谢谢!
小智 38
Michal Politowski 的评论完全正确。我使用此方法在部署新工件时自动重新启动服务。这是非常有帮助的。
要清楚,您需要:
服务端
[Unit]
Description=srv 0.1: Service's description
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/srv
ExecStart=/opt/srv/bin/srv
User=root
Group=root
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
srv-watcher.service
[Unit]
Description=srv restarter
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart srv.service
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
srv-watcher.path
[Path]
PathModified=/opt/srv/lib
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
Dre*_*rew 10
上面的答案几乎很好,但它遗漏了一些我花了一段时间才弄清楚的事情。其他人遇到了同样的问题,请参阅评论部分。
systemctl enable srv-watcher.{path,service}
systemctl start srv-watcher.{path,service}
Run Code Online (Sandbox Code Playgroud)
服务端
[Unit]
Description=srv 0.1: Service's description
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/srv
ExecStart=/opt/srv/bin/srv
User=root
Group=root
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
srv-watcher.service
[Unit]
Description=srv restarter
After=network.target
StartLimitIntervalSec=10
StartLimitBurst=5
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart srv.service
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
srv-watcher.path
[Path]
Unit=srv-watcher.service
PathChanged=/opt/srv/lib
# trigger on changes to a file not just create/delete
# don't put comments in the same line as the command
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
如果它正常工作,您将在 journalctl 中看到这些消息
$: journalctl -f -o cat -u srv-watcher
Starting srv-watcher...
Started srv-watcher.
Run Code Online (Sandbox Code Playgroud)
其他需要注意的事情是,补丁可能会多次触发,但 srv-watcher.service 将遵守以 10 秒为间隔重新启动 5 次的默认限制。https://www.freedesktop.org/software/systemd/man/systemd.unit.html#StartLimitIntervalSec=interval
归档时间: |
|
查看次数: |
17662 次 |
最近记录: |