From 64ce8bc9d88407cc2d7425274b2d630766738d15 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch> Date: Thu, 22 Sep 2022 13:56:46 +0200 Subject: [PATCH] fix: behaviur when trigger command fails --- README.md | 4 +-- docs/_index.md | 4 +-- onfilechange.sh | 69 +++++++++++++++++++++++-------------------------- 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 2919c77..c735b0c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # onfilechange -A Shell script that watches a given file or multiple files. -If one of the watched file changes then a given command will be exxecuted. +A Shell script that watches a given fileobject or multiple fileobjects (=files or directories). +If one of the watched fileobject changes then a given command will be exxecuted. It loops permanently; you need to stop it by Ctrl + C and/ or can use it as systemd watcher daemon. diff --git a/docs/_index.md b/docs/_index.md index 2919c77..c735b0c 100644 --- a/docs/_index.md +++ b/docs/_index.md @@ -1,7 +1,7 @@ # onfilechange -A Shell script that watches a given file or multiple files. -If one of the watched file changes then a given command will be exxecuted. +A Shell script that watches a given fileobject or multiple fileobjects (=files or directories). +If one of the watched fileobject changes then a given command will be exxecuted. It loops permanently; you need to stop it by Ctrl + C and/ or can use it as systemd watcher daemon. diff --git a/onfilechange.sh b/onfilechange.sh index 724c164..0d6f8f6 100755 --- a/onfilechange.sh +++ b/onfilechange.sh @@ -5,8 +5,9 @@ # T R I G G E R C O M M A N D O N A F I L E C H A N G E # # -# A Shell script that watches a given file or multiple files. -# If the file changes then a given command will be exxecuted. +# A Shell script that watches a given fileobject or multiple fileobjects +# (=files or directories) +# If the fileobject changes then a given command will be exxecuted. # It loops permanently; you need to stop it by Ctrl + C and/ or can # use it as systemd watcher daemon. # @@ -15,11 +16,13 @@ # # licence: GNU GPL 3.0 # source: https://git-repo.iml.unibe.ch/iml-open-source/onfilechange +# docs: https://os-docs.iml.unibe.ch/onfilechange/ # # ---------------------------------------------------------------------- # 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 -# 2022-03-11 v1.03 <axel.hahn@iml.unibe.ch> shell fixes +# 2022-03-11 v1.04 <axel.hahn@iml.unibe.ch> shell fixes +# 2022-03-11 v1.05 <axel.hahn@iml.unibe.ch> fix: behaviur when trigger command fails; shell fixes; update docs # ====================================================================== # ---------------------------------------------------------------------- @@ -33,7 +36,7 @@ sWatchFile= sMode= # ---- below are some internal variables -sVersion=1.04 +_version=1.05 # ---------------------------------------------------------------------- @@ -45,14 +48,15 @@ function showHelp(){ local _self=$( basename $0 ) cat <<ENDOFHELP HELP: - This script checks the change of a given file and triggers + This script checks the change of a given fileobjects and triggers a command if it changes PRAMETERS: -c [command] command to execute on a file change - -f [filename(s)] - filenames to watch; separate multiple files with space and put all in quotes + -f [fileobject(s)] + filenames or directories to watch; separate multiple files with + space and put all in quotes -h show this help -i @@ -62,7 +66,8 @@ PRAMETERS: -v verbose mode; enable showing debug output -w [integer] - for stat 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: $_self -f /home/me/touchfile.txt -c "ls -l" @@ -72,7 +77,8 @@ EXAMPLES: watch touchfile.txt and touchfile2.txt $_self -f /home/me/touchfile.txt -s -w 10 -c "echo hello" - watch touchfile.txt every 10 sec with stat and show "hello" on a change + watch touchfile.txt every 10 sec with stat and show "hello" on a + change ENDOFHELP @@ -125,21 +131,19 @@ function initFilestatus(){ function compareFilestatus(){ getFilestatus >${sTmpFile2} wd "$(cat ${sTmpFile2})" - diff ${sTmpFile} ${sTmpFile2} - if [ $? -eq 0 ]; then + if diff ${sTmpFile} ${sTmpFile2}; then wd "No Change" else wd "Change detected." - execCommand - if [ $? -eq 0 ]; then + if execCommand; then echo Command was successful. - echo - wd "Re-Init File status" - # initFilestatus - mv ${sTmpFile2} ${sTmpFile} else - echo FAILED. + echo rc=$? FAILED. fi + wd "Re-Init File status" + mv ${sTmpFile2} ${sTmpFile} + echo + echo ">>>>> waiting for the next change ..." fi } @@ -147,8 +151,6 @@ function compareFilestatus(){ # # global (string) $sCommand command line to exectute function execCommand(){ - listFiles - echo echo ">>>>> $(date) Executing ${sCommand} ..." ${sCommand} } @@ -160,25 +162,22 @@ function execCommand(){ cat <<ENDOFHEAD ______________________________________________________________________________ - T R I G G E R C O M M A N D O N A F I L E C H A N G E -______________________________________________________________________________ - v${sVersion} + T R I G G E R C O M M A N D O N A F I L E C H A N G E +_______________________________________________________________________| v${_version} ENDOFHEAD -which stat >/dev/null 2>&1 -if [ $? -ne 0 ]; then +if which stat >/dev/null 2>&1; then + echo "INFO: stat command detected" + sMode=stat +else echo ERROR: the command stat was not found on your system. - which inotifywait >/dev/null 2>&1 - if [ $? -ne 0 ]; then + if ! which inotifywait >/dev/null 2>&1; then echo ERROR: the command inotifywait was not found on your system. exit 2 fi echo "INFO: enabling inotifywait command" sMode=inotifywait -else - echo "INFO: stat command detected" - sMode=stat fi if [ $# -eq 0 ]; then @@ -189,7 +188,7 @@ fi while getopts ":c: :v :f: :h :i :s :w:" opt do case $opt in - \?|h) + h) showHelp exit 0 ;; @@ -246,8 +245,7 @@ if [ -z "${sWatchFile}" ]; then echo ERROR: set a check file with param -f exit 1 fi -listFiles -if [ $? -ne 0 ]; then +if ! listFiles; then echo "INFO: file ${sWatchFile} (or one of them) does not exist yet" # echo "ERROR: file ${sWatchFile} (or one of them) does not exist yet" # exit 1 @@ -271,14 +269,11 @@ sTmpFile2="/tmp/$(basename $0)-${myset}-current.tmp" case $sMode in "inotifywait") while true; do - listFiles >/dev/null 2>&1 - if [ $? -eq 0 ]; then + if listFiles >/dev/null 2>&1; then inotifywait -e attrib -e modify "${sWatchFile}" && execCommand else echo "ERROR: inotifywait only can notify if all watched files exist." echo "Use parameter -s to use stat for file detection, This mode also allows that a file is deleted." - wd sleep ${iSleep} - sleep ${iSleep} exit 2 fi done -- GitLab