I need to trigger a script whenever a new file shows up in a directory. The old implementation uses incrontab. To add an inotify watch via incrontab I simply add a line per directory as follows

/dir/to/watch IN_CLOSE_WRITE /script/to/call $@/$#

Whenever a file is written, my script is called with an argument - a path to the newly created file.

I am switching from incrontab directory watcher to systemd path unit:


When the corresponding service unit is called I need to somehow retrieve the name of the new file that triggered the event. The closest I found was to use TRIGGER_PATH variable initialized by the path unit. The problem is that the variable is set to /dir/to/watch directory and not the newly created file.

I need to know the specific file name, but I can't find a way to get if from systemd.

  • I'm not sure if this is possible. What are you doing with the files that trigger your service? If they can moved into a new directory, you know that anything in the watched directory hasn't been processed yet. This answer has some suggestions, but that's only appropriate if you're watch for specific files.
    – larsks
    Commented Jul 2, 2023 at 21:01
  • @larsks It is a directory with configuration files, so moving them out is not an option. Commented Jul 3, 2023 at 4:31
  • github.com/systemd/systemd/issues/28939
    – Jay M
    Commented Sep 9, 2023 at 23:37


You must log in to answer this question.

Browse other questions tagged .