Score:0

Avoid missing changes with PathChanged while unit is running

bo flag

I want to monitor a file and run a command that applies the content of the file to my system, whenever the file it changes. I'm currently using a path unit triggering a service unit to do that. In the example below, the command simply copies the contents of /somefile to /otherfile:

# /etc/systemd/system/example.unit
[Service]
ExecStart=/usr/bin/cp /somefile /otherfile
# /etc/systemd/system/example.path
[Path]
PathChanged=/somefile

[Install]
WantedBy=default.target

The problem I'm facing is that /somefile might be changed/replaced while the command is running. To make sure that /otherfile always ends up with the same content as /somefile, the service would need to be restarted after it completes in that case, but I couldn't find a directive for path units that does that.

Even if the command would check that /somefile hasn't changed while copying, there is a window between that check and the service unit terminating, where the file could be changed without the unit being restarted.

How can I implement this using systemd?

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.