Maverickgugu
Maverickgugu

Reputation: 777

How to notify a linux (systemd) process to take action, when a file is created

Not sure where to start on the above question.

I have systemd process (A) running with some file handling capabilities.

The requirement is to notify (A) when a file is created by another process (B) at a certain location (say /tmp/abc.txt).

Upon receiving the notification, (A) can open the file and do its parsing/caching and close it. I believe we can queue the requests if (A) is already processing something.

Is this requirement even possible? If yes, is there a traditional/systemd design pattern to go about it? If not, can this be achieved in any alternate way?

Upvotes: 1

Views: 1125

Answers (1)

Lucas Werkmeister
Lucas Werkmeister

Reputation: 2752

You can either use the inotify API, as Tesseract mentioned, or employ a systemd path unit (which also uses inotify under the hood):

# foo.path
[Path]
PathExists=/tmp/abc.txt

# foo.service
[Service]
ExecStart=/usr/local/bin/A

Note that this will start foo.service each time the file is created, not notify it. From your question it’s not clear to me if that’s okay for your case or not – if you want to do some operation on the file and then do nothing until the next time the file pops up, that should be fine (exit from the service and set Type=oneshot in that case).

Upvotes: 3

Related Questions