What is systemd.path
systemd.path works similar to auditd in earlier versions with extra features as triggering an event or a run a script when a change is made to file or directory.
From the man page of systemd.path:
How to configure systemd.path file
Following are the steps to create systemd.path file:
1. Create systemd path and service file at path “/etc/systemd/system/” as follow
– Create systemd service file:
# cat mymonitor.service [Unit] Description= Starts the logging script [Service] Type=oneshot ExecStart=/home/test/mymonitor.sh
– Create systemd path file:
# cat mymonitor.path [Unit] Description= Triggers the service that logs changes. [Path] PathModified=/home/test/my/ [Install] WantedBy=multi-user.target
Whenever any changes made to the directory “/home/test/my/” (refer to above “mymonitor.path”), the service file against it is triggered that triggers defined script against “ExecStart” which is defined in systemd service file under [Service] section, here it is “/home/test/bin/mymonitor.sh”.
2. Create the script that needs to be triggered
– Create a custom script(here /home/test/mymonitor.sh) and add all the event that needs to trigger if any changes made at directory defined against PathModified under [Path] section of the systemd path file.
# cat /home/test/mymonitor.sh #!/bin/bash echo `date` >> /home/test/fileMonitor.logo ; `ll /home/test/my/` >> /home/test/fileMonitor.log echo "===============================" >> /home/test/fileMonitor.log
– Above script that captures timestamps at which the changes made to the directory “/home/test/my/” and output the content of the directory to “/home/test/fileMonitor.log“.
3. Grant execute permission to script:
Ensure that the script /home/test/mymonitor.sh is executible:
# chmod gou=r-x /home/test/mymonitor.sh
4. Start and enable systemd .service and .path file
Start and enable created service and path file (mymonitor.service and mymonitor.path) to make the changes effective:
# systemctl start mymonitor.path # systemctl start mymonitor.service # systemctl enable mymonitor.path # systemctl enable mymonitor.service
5. Reload systemd daemon in order to aware systemd with the created path and service file
# systemctl daemon-reload
For more options on systemd.path please refer systemd.path man page.
# man systemd.path