How to run a script when a directory is changed by another user?
Using inoticoming
:
You can put a script in /etc/init.d/
that runs inoticoming
at boot time.
Create a new folder to hold the
inoticoming
log / lastpid
for thewatched
folder:sudo mkdir -p /var/log/inoticoming/watched/
Create a script
inoticoming_watched
in/etc/init.d/
:
* Remember to change <path_to_folder> and <path_to_script> to match the full path of the watched
folder and the full path of the script to execute
#!/bin/sh
case "${1}" in
start)
inoticoming --logfile '/var/log/inoticoming/watched/inoticoming.log' --pid-file '/var/log/inoticoming/watched/inoticoming_last_pid.txt' <path_to_folder> <path_to_script> \;
;;
stop)
kill -15 $(< /var/log/inoticoming/watched/inoticoming_last_pid.txt tee)
;;
restart)
${0} stop
sleep 1
${0} start
;;
*)
echo "Usage: ${0} {start|stop|restart}"
exit 1
;;
esac
Mark the script as executable:
sudo chmod u+x /etc/init.d/inoticoming_watched
Make sure that the script called by
inoticoming_watched
is executable.Update
rc.d
to make the serviceinoticoming_watched
start at boot time:sudo update-rc.d inoticoming_watched defaults
You can check the inoticoming
log in /var/log/inoticoming/watched
.
First of all, install inoticoming:
sudo apt-get install inoticoming
Then use this command:
Pay attention to ongoing processes of inoticoming, because they can be started multiple times.
$ inoticoming /home/user1/watched /usr/local/bin/syncbh.sh /home/user1/watched/{} \;
^ ^ ^
| | |
^-- The directory to be monitored |
| |
^-- Your script |
^-- The parameter for your script
The process runs in backgound and is watching
/home/user1/watched
-
When a file is added or changed in that directory, the script
/usr/local/bin/syncbh.sh
is called.The parameter for this script is in this case
/home/user1/watched/<name_of_changed_or_modified_file>
{}
is replaced by the filename