Skip to content
Snippets Groups Projects
Commit b4a936dc authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

use stat as default

parent d60dc056
No related branches found
No related tags found
No related merge requests found
...@@ -10,14 +10,15 @@ ...@@ -10,14 +10,15 @@
# It loops permanently; you need to stop it by Ctrl + C and/ or can # It loops permanently; you need to stop it by Ctrl + C and/ or can
# use it as systemd watcher daemon. # use it as systemd watcher daemon.
# #
# It uses stats for wide compatibility but prefers inotifywatch to # It uses stat for wide compatibility but can enable inotifywatch to
# check a file change. # check a file change by an event.
# #
# licence: GNU GPL 3.0 # licence: GNU GPL 3.0
# source: https://git-repo.iml.unibe.ch/iml-open-source/onfilechange # source: https://git-repo.iml.unibe.ch/iml-open-source/onfilechange
# #
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2019-10-14 v1.0 <axel.hahn@iml.unibe.ch> first basic version # 2019-10-14 v1.0 <axel.hahn@iml.unibe.ch> first basic version
# 2019-10-21 v1.03 <axel.hahn@iml.unibe.ch> use stat as default
# ====================================================================== # ======================================================================
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -31,7 +32,7 @@ sWatchFile= ...@@ -31,7 +32,7 @@ sWatchFile=
sMode= sMode=
# ---- below are some internal variables # ---- below are some internal variables
sVersion=1.02 sVersion=1.03
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -55,11 +56,11 @@ PRAMETERS: ...@@ -55,11 +56,11 @@ PRAMETERS:
-i -i
force inotifywait command force inotifywait command
-s -s
force stats command force stat command (default mode)
-v -v
verbose mode; enable showing debug output verbose mode; enable showing debug output
-w [integer] -w [integer]
for stats mode: wait time in seconds betweeen each test or on missing file; default: 5 sec for stat mode: wait time in seconds betweeen each test or on missing file; default: 5 sec
EXAMPLES: EXAMPLES:
`basename $0` -f /home/me/touchfile.txt -c "ls -l" `basename $0` -f /home/me/touchfile.txt -c "ls -l"
...@@ -68,8 +69,8 @@ EXAMPLES: ...@@ -68,8 +69,8 @@ EXAMPLES:
`basename $0` -f "/home/me/touchfile.txt home/me/touchfile2.txt" -c "ls -l" `basename $0` -f "/home/me/touchfile.txt home/me/touchfile2.txt" -c "ls -l"
watch touchfile.txt and touchfile2.txt watch touchfile.txt and touchfile2.txt
`basename $0` -f /home/me/touchfile.txt -s -w 10 -c "ls -l" `basename $0` -f /home/me/touchfile.txt -s -w 10 -c "echo hello"
watch touchfile.txt every 10 sec with stats and make a file listing on change watch touchfile.txt every 10 sec with stat and show "hello" on a change
ENDOFHELP ENDOFHELP
...@@ -94,7 +95,7 @@ function listFiles(){ ...@@ -94,7 +95,7 @@ function listFiles(){
ls -ld ${sWatchFile} 2>&1 ls -ld ${sWatchFile} 2>&1
} }
# for stats: helper to get current file status # for stat: helper to get current file status
# #
# global (string) $sWatchFile # global (string) $sWatchFile
function getFilestatus(){ function getFilestatus(){
...@@ -104,7 +105,7 @@ function getFilestatus(){ ...@@ -104,7 +105,7 @@ function getFilestatus(){
done done
} }
# for stats: inititalize file change detection # for stat: inititalize file change detection
# #
# global (string) $TmpFile last/ initial file status # global (string) $TmpFile last/ initial file status
# global (string) $sTmpFile2 current file status # global (string) $sTmpFile2 current file status
...@@ -114,7 +115,7 @@ function initFilestatus(){ ...@@ -114,7 +115,7 @@ function initFilestatus(){
cp -p ${sTmpFile} ${sTmpFile2} cp -p ${sTmpFile} ${sTmpFile2}
} }
# for stats: compare file status and execute command on change # for stat: compare file status and execute command on change
# #
# global (string) $TmpFile last/ initial file status # global (string) $TmpFile last/ initial file status
# global (string) $sTmpFile2 current file status # global (string) $sTmpFile2 current file status
...@@ -163,19 +164,19 @@ ______________________________________________________________________________ ...@@ -163,19 +164,19 @@ ______________________________________________________________________________
ENDOFHEAD ENDOFHEAD
which inotifywait >/dev/null 2>&1 which stat >/dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo ERROR: the command inotify was not found on your system, echo ERROR: the command stat was not found on your system.
which stat >/dev/null 2>&1 which inotifywait >/dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo ERROR: the command stat was not found on your system, echo ERROR: the command inotifywait was not found on your system.
exit 2 exit 2
fi fi
echo "INFO: enabling compatibility mode with stats command" echo "INFO: enabling inotifywait command"
sMode=stats
else
echo "INFO: inotifywait command detected"
sMode=inotifywait sMode=inotifywait
else
echo "INFO: stat command detected"
sMode=stat
fi fi
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
...@@ -207,8 +208,8 @@ do ...@@ -207,8 +208,8 @@ do
wd "forcing mode with inotifywait command" wd "forcing mode with inotifywait command"
;; ;;
s) s)
sMode=stats sMode=stat
wd "forcing mode with stats command" wd "forcing mode with stat command"
;; ;;
w) w)
typeset -i iSleep=$OPTARG typeset -i iSleep=$OPTARG
...@@ -222,10 +223,12 @@ do ...@@ -222,10 +223,12 @@ do
done done
cat <<ENDOFINFO cat <<ENDOFINFO
--- summary
checking file [${sWatchFile}] checking file [${sWatchFile}]
with command [${sMode}] with command [${sMode}]
with a sleep time of [${iSleep}] seconds with a sleep time of [${iSleep}] seconds
and on change start command [${sCommand}] and on change I start the command [${sCommand}]
............................................................................... ...............................................................................
...@@ -271,7 +274,7 @@ case $sMode in ...@@ -271,7 +274,7 @@ case $sMode in
inotifywait -e attrib -e modify ${sWatchFile} && execCommand inotifywait -e attrib -e modify ${sWatchFile} && execCommand
else else
echo "ERROR: inotifywait only can notify if all watched files exist." echo "ERROR: inotifywait only can notify if all watched files exist."
echo "Use parameter -s to use stats for file detection, This mode also allows that a file is deleted." echo "Use parameter -s to use stat for file detection, This mode also allows that a file is deleted."
wd sleep ${iSleep} wd sleep ${iSleep}
sleep ${iSleep} sleep ${iSleep}
exit 2 exit 2
...@@ -279,7 +282,7 @@ case $sMode in ...@@ -279,7 +282,7 @@ case $sMode in
done done
;; ;;
"stats") "stat")
wd "--- initial read of watched files" wd "--- initial read of watched files"
initFilestatus initFilestatus
echo waiting for file changes ... echo waiting for file changes ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment