Table of Contents

Use as systemd service

With running the script as a service can be helpful if you administrate servers and habe developer teams with limited access. Instead of giving sudo permissions you can watch a touchfile to execute something specific as root, i.e. restart Apache httpd. In the real world this file would be touched during an automated deploy process.

Below is just an example service watching a dummy file in /tmp directory.

In /etc/systemd/system/ create a service config file onfilechange-demo.service

[Unit]
Description=Onfilechange demo
Wants=multi-user.target

[Service]
ExecStart=/usr/local/bin/onfilechange.sh -f "/tmp/mytestfile" -c 'systemctl restart apache2'
Restart=on-failure
RestartSec=5s
SyslogIdentifier=onfilechange
User=root
Group=root
Type=simple

[Install]
WantedBy=multi-user.target

Check if the service exists systemctl status onfilechange-demo. If so you can start and stop it with systemctl [start|stop] onfilechange-demo

To start the service on reboot you additionally need to enable it: systemctl enable onfilechange-demo.