Can systemd restart a service when a specific binary on the server is updated?

Viewed 893

On Ubuntu 18.04 I have Unattended Upgrades update apps regularly including a 3rd party PPA that installs a binary /usr/bin/some_app. My systemd unit file runs that service via ExecStart=/usr/bin/some_app. I can verify the updates work on schedule in /var/log/apt/history.log.

However even when the binary is updated via Unattended Upgrades systemd doesn't restart the app, I assume because some_app is started via a custom unit file unrelated to that PPA. So from the cli some_app --version shows v2.0.0 but systemd is still running v1.0.0.

Does systemd have a method to track a file or detect the binary referenced in ExecStart has changed on disk and it should restart? A backup hack for me would be using RuntimeMaxSec= which would get the job done, but I was hoping something more elegant existed

1 Answers

You could try adding a .path unit (man systemd.path) to watch for a close() after write() change to the file, which then restarts your app service. Not tested:

/etc/systemd/system/myappwatch.path

[Unit]
Description=watch for changed file
[Path]
PathChanged=/usr/bin/some_app
#Unit=myappwatch.service

/etc/systemd/system/myappwatch.service

[Service]
ExecStart=/bin/systemctl restart myapp.service

You might be able to replace the systemctl restart with something magic like Conflicts=myapp but I'll let you experiment. You also need to enable the .path unit as usual with an appropriate WantedBy=. I'm not sure what happens if the path is a symbolic link, so perhaps you should resolve the path to the real file is that is the case.

Related