Wir möchten gern das Home-Verzeichnis eines sftp tux Benutzers überwachen und wenn neue Datei(en) erstellt oder verschoben werden, sollen Gruppen Schreibrechte darauf gesetzt werden. Zuerst erstellen wir einen Service wie folgt.
root:~# vi /etc/systemd/system/inotify_tux.service
Die Service Datei /etc/systemd/system/inotify_tux.service sieht wie folgt aus.
[Unit]
Description=Monitoring the tux directory and changing the permissions for new files
[Service]
Type=oneshot
ExecStart=/bin/sh -c '/usr/local/sbin/inotify.sh "/home/tux" &'
ExecStop=/bin/sh -c "kill $(ps -fade | grep 'inotify.sh /home/tux' | grep -v grep | awk '{print $2}')"
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Nun aktivieren wir den Service.
root:~# cd /etc/systemd/system/multi-user.target.wants/ && ln -s /etc/systemd/system/inotify_tux.service
root:~# systemctl daemon-reload
Nun erstellen wir das im Service definierte Skript.
root:~# vi /usr/local/sbin/inotify.sh
Der Inhalt des Skripts /usr/local/sbin/inotify.sh
#!/bin/bash
# Install the package inotify-tools to use the inotifywait command
dir="$1"
inotifywait -mrq -e move -e create --format %w%f $dir | while read FILE; do
chmod g+w "$FILE"
done
Als letzten Schritt machen wir das Skript ausführbar und starten den Service.
root:~# chmod 755 /usr/local/sbin/inotify.sh
root:~# systemctl start inotify_tux