Leaving incrond for systemd.path

I’ve been having problems with incrond for months; after trying to upgrade from version 0.5.10, incrond would not trigger after a specified event occurred in the monitored path.

After upgrading to version 0.5.12-9 (I couldn’t use 0.5.10 because of security policies), incrond has started behaving in a completely random way: sometimes it was triggered as expected but some other times (way too many), nothing was happening.

This was the entry in incrontab:

/home/elena/upload IN_CLOSE_WRITE /bin/find $@ -type f -exec /bin/chmod g+rw {} \+

Writing a script and adding it to crontab was out of question; here is where systemd came in handy, with path units.

A .path unit (systemd.path) monitors a file or directory and it calls a .service unit (systemd.service), usually with the same name, when something happens to the monitored file or directory.

In my case, I simply had to fix group permission for files in a specific directory.

fixperm.path

[Unit]
Description=changing group permissions

[Path]
PathModified=/home/compuk/upload
PathModified=/home/compuk/upload/backup

Note: systemd.path is not recursive! You must specify every path you need to monitor.

fixperm.service

[Unit]
Description=service for fixperm.path

[Service]
Type=oneshot
User=elena

ExecStart=/usr/bin/find /home/compuk/upload -type f -exec /bin/chmod -R g+rw {} \;
ExecStart=/usr/bin/find /home/compuk/upload/backup -type f -exec /bin/chmod -R g+rw {} \;

Note: I didn’t use RemainAfterExit=Yes in the service unit because, if the service doesn’t die (inactive) every time, the .path unit will not trigger properly.