Want a service restarted when its config file changes, or a script run when something lands in a folder? You don’t need a loop with sleep for that. systemd has a unit type for it, and I think it’s one of the least known ones.
A .path unit watches paths with inotify and starts a service when they change. By default foo.path starts foo.service. Two small files, here to restart an app whenever its config is saved:
# /etc/systemd/system/myapp-reload.path
[Path]
PathChanged=/etc/myapp/config.yml
[Install]
WantedBy=paths.target
# /etc/systemd/system/myapp-reload.service
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart myapp.service
Then turn on the watcher, not the service:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp-reload.path
PathChanged= fires when a file that was open for writing gets closed, so an editor saving the file triggers it once. PathModified= also fires on plain writes, which can mean many triggers for one save. For an inbox folder, DirectoryNotEmpty=/srv/inbox starts the service whenever at least one file is in there; let the service move the files out, otherwise it keeps getting triggered. Unit= lets you start a service with a different name.
Caveats: inotify only sees changes made on this machine, so a folder on an NFS share that another host writes to won’t trigger anything. Files whose name starts with a dot are generally ignored. And if the service keeps getting triggered, the start rate limit of the service kicks in and the path unit fails too, which ends the loop. That’s good protection, but it means a busy folder needs a service that empties it fast.
Further reading: systemd.path(5) manual page, systemd.special(7) manual page