5

I've got a file that is periodically written to by a service running on an ubuntu server. I've got another service currently running on a timer that checks the written file for changes from previous states and updates a mysql database table if changes are made. I'd like to setup some kind of service that can immediately tell when a change to the file is made instead of waiting for a timer to fire, but I haven't seen how to set a file watch using systemd.

2

1 Answer 1

6

The systemd mechanism for this is a path unit, in which you name the file to watch in either a PathChanged or a PathModified setting, depending from how the writer is writing to the file.

If it is writing the whole file and closing it each time, then PathChanged avoids a pitfall of PathModified, which is liable to premature activation if the modification as a whole involves multiple writes writing just a part of the file. But, on the other hand, PathChanged is liable to be triggered even if the file was only opened for writing by a writer but never actually written to.

You either name the path unit the same as an associated service unit or use the Unit setting. This associated service unit will of course be your service that transfers the file contents to the database, sans the superfluous timer. When the relevant change in the file happens, the path unit causes the service unit to be activated (if it isn't already). This is so-called path activation.

Further reading

  • Lennart Poettering et al. (2017). systemd.unit. systemd manual pages. freedesktop.org.
  • Lennart Poettering et al. (2017). systemd.path. systemd manual pages. freedesktop.org.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .