Monitor file system events to restart service on every configuration change.
I will use varnish
service and wait for changes to the /etc/varnish/default.vcl
configuration file before initiating restart operation.
Install varnish
web application accelerator.
$ sudo apt install varnish
Install inotify-tools
package that provides inotifywait
utility that waits for changes to the specific file and exit once the desired event occurs.
$ sudo apt install inotify-tools
Create a simple service that will make use of the utility as mentioned above.
$ cat << EOF | sudo tee /etc/systemd/system/restart-varnish-on-configuration-change.service [Service] ExecStart=/usr/bin/inotifywait -e modify /etc/varnish/default.vcl Restart=always RestartSec=10 ExecStop=/usr/bin/systemctl restart varnish [Install] WantedBy=multi-user.target [Unit] Description=Restart varnish on configuration change Requires=varnish.service After=varnish.service EOF
Reload the systemd manager configuration.
$ sudo systemctl daemon-reload
Enable and start created service.
$ sudo systemctl enable --now restart-varnish-on-configuration-change
Inspect varnish
service status to see that it is running normally.
$ systemctl status varnish
● varnish.service - Varnish HTTP accelerator Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-09-19 22:54:25 GMT; 31min ago Docs: https://www.varnish-cache.org/docs/6.1/ man:varnishd Main PID: 9170 (varnishd) Tasks: 217 (limit: 1149) Memory: 54.4M CGroup: /system.slice/varnish.service ├─9170 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m └─9221 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
Inspect restart-varnish-on-configuration-change
service status to see that it is waiting for a specific event.
$ systemctl status restart-varnish-on-configuration-change.service
● restart-varnish-on-configuration-change.service - Restart varnish on configuration change Loaded: loaded (/etc/systemd/system/restart-varnish-on-configuration-change.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-09-19 23:18:24 GMT; 8min ago Main PID: 13331 (inotifywait) Tasks: 1 (limit: 1149) Memory: 244.0K CGroup: /system.slice/restart-varnish-on-configuration-change.service └─13331 /usr/bin/inotifywait -e modify /etc/varnish/default.vcl
Alter varnish
configuration file.
$ echo | sudo tee -a /etc/varnish/default.vcl
Inspect restart-varnish-on-configuration-change
service status to see that it was restarted.
$ systemctl status restart-varnish-on-configuration-change.service
● restart-varnish-on-configuration-change.service - Restart varnish on configuration change Loaded: loaded (/etc/systemd/system/restart-varnish-on-configuration-change.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-09-19 23:27:00 GMT; 8s ago Main PID: 13688 (inotifywait) Tasks: 1 (limit: 1149) Memory: 244.0K CGroup: /system.slice/restart-varnish-on-configuration-change.service └─13688 /usr/bin/inotifywait -e modify /etc/varnish/default.vcl
Inspect restart-varnish-on-configuration-change
logs to know that it was restarted after a particular event occurred.
$ sudo journalctl -u restart-varnish-on-configuration-change.service
-- Logs begin at Sat 2020-09-19 22:38:39 GMT, end at Sat 2020-09-19 23:34:36 GMT. -- Sep 19 23:18:24 debian systemd[1]: Started restart-varnish-on-configuration-change.service. Sep 19 23:18:24 debian inotifywait[13331]: Setting up watches. Sep 19 23:18:24 debian inotifywait[13331]: Watches established. Sep 19 23:26:49 debian inotifywait[13331]: /etc/varnish/default.vcl MODIFY Sep 19 23:26:50 debian systemd[1]: restart-varnish-on-configuration-change.service: Succeeded. Sep 19 23:27:00 debian systemd[1]: restart-varnish-on-configuration-change.service: Service RestartSec=10s expired, scheduling restart. Sep 19 23:27:00 debian systemd[1]: restart-varnish-on-configuration-change.service: Scheduled restart job, restart counter is at 1. Sep 19 23:27:00 debian systemd[1]: Stopped Restart varnish on configuration change. Sep 19 23:27:00 debian systemd[1]: Started Restart varnish on configuration change. Sep 19 23:27:00 debian inotifywait[13688]: Setting up watches. Sep 19 23:27:00 debian inotifywait[13688]: Watches established.
Inspect varnish
service status to confirm that it was restarted.
$ systemctl status varnish
● varnish.service - Varnish HTTP accelerator Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-09-19 23:26:50 GMT; 29s ago Docs: https://www.varnish-cache.org/docs/6.1/ man:varnishd Main PID: 13460 (varnishd) Tasks: 217 (limit: 1149) Memory: 57.4M CGroup: /system.slice/varnish.service ├─13460 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m └─13472 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
It is excellent for testing when you expose configuration file using nginx/web-dav
to upload it using curl
.