I use Ubuntu 18.04. I created service and path unit for my Sneakers workers process.
sneakers.service:
[Unit]
Description=sneakers
After=syslog.target network.target
[Service]
Type=simple
WorkingDirectory=/home/deploy/rails_app/current
ExecStart=/home/deploy/.rbenv/bin/rbenv exec bundle exec rails sneakers:run
User=deploy
Group=deploy
UMask=0002
Environment=MALLOC_ARENA_MAX=2
Environment=RAILS_ENV=production
RestartSec=1
Restart=on-failure
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=sneakers
[Install]
WantedBy=multi-user.target
And sneakers.path
[Path]
PathModified=/home/deploy/rails_app/current/tmp/restart.txt
[Install]
WantedBy=multi-user.target
Then I enabled them and started path unit:
sudo systemctl enable sneakers.{path,service}
sudo systemctl start sneakers.path
It starts ok, service script works fine. But it does not restarts when file restart.txt is changed (I tried touching it and modifying).
I tried step-by-step tutorial https://www.redhat.com/sysadmin/introduction-path-units – and it works fine on my system. The difference is that this tutorial uses some short running (one time) script that exists after execution. While my service uses long running foreground task in ExecStart.
How can I make my service work and restart with file touching?
Best Answer
systemd.path
units do not restart services, but only start them. Here is a method where you define new "watcher" service unit and path unit that will start and restart your service(s): https://superuser.com/questions/1171751/restart-systemd-service-automatically-whenever-a-directory-changes-any-file-ins