User Tools

Site Tools


os:linux:file_watcher_service

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
os:linux:file_watcher_service [2020-12-05 00:49] – created Manuel Freios:linux:file_watcher_service [2020-12-05 11:30] (current) Manuel Frei
Line 16: Line 16:
 #!/usr/bin/env bash #!/usr/bin/env bash
 file_name="/tmp/watched_file.txt" file_name="/tmp/watched_file.txt"
-log_file="/tmp/$(basename ${0}).log"+log_file="/tmp/$(basename "${0}").log" 
 +lock_file="/tmp/$(basename "${0}").lock" 
 + 
 +# check & set lock 
 +exec 200>${lock_file} 
 +flock -n 200 || { echo "Another instance of this script is already running. Abort."; exit 1; } 
 +PID=$$ 
 +echo ${PID} 1>&200 
 + 
 +# file watch
 while true; do while true; do
     inotifywait -e modify ${file_name}     inotifywait -e modify ${file_name}
     echo "[$(date --iso-8601=seconds)] change detected on file ${file_name}" >> ${log_file}     echo "[$(date --iso-8601=seconds)] change detected on file ${file_name}" >> ${log_file}
-    # maybe do some other actions+    # maybe do some other actions after a modification
 done done
 </code> </code>
Line 49: Line 58:
 systemctl daemon-reload systemctl daemon-reload
 systemctl start file_watcher.service systemctl start file_watcher.service
 +</code>
 +
 +If you need to create some form of monitoring script/actions, maybe this snippets will help you:
 +<code>
 +[root@server ~]# systemctl is-active file_watcher.service >/dev/null && echo OK || echo NOK
 +OK
 +[root@server ~]# systemctl stop file_watcher.service 
 +[root@server ~]# systemctl is-active file_watcher.service >/dev/null && echo OK || echo NOK
 +NOK
 </code> </code>
os/linux/file_watcher_service.1607125743.txt.gz · Last modified: 2020-12-05 00:49 by Manuel Frei