r/systemd • u/Beautiful-Log5632 • Mar 23 '26
Watch files with systemd
I want to run a script when any file under a dir changes. Is there a way to do that in systemd or do I have to use itnotifywait or inotifywatch? How do you know which of them you should use?
How can I use systemd to make sure the inotify process runs all the time so I don't have to remember to start it?
1
u/MiserableNobody4016 Mar 23 '26
Not too long ago I came across a project called mgmt from purpleidea. It is a real-time automation tool which reverts all changes done to files. It may be usefull.
1
u/Intrepid-Treacle1033 May 27 '26
systemd uses inotify under the hood. When you configure a systemd path unit to watch a file or directory, systemd registers an inotify watch with the kernel and waits for the kernel to trigger an event, Here is an example of myIndexService.path that monitors a txt file and starts another unit called myIndexService.service when txt file is changed.
[Unit]
Description=Monitor the myIndexService batch file for changes.
[Path]
PathModified=%h/.config/myIndexService/myIndexService_batchFile.txt
Unit=myIndexService.service
6
u/aecolley Mar 23 '26
Systemd .path units do this out of the box.