From e77a8ad931396acb28416236769e24b88abbf152 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch> Date: Fri, 11 Mar 2022 08:20:24 +0100 Subject: [PATCH] v1.04: shell fixes; update readme --- README.md | 70 +++++++++++++++++++++++++++++++++++++------------ onfilechange.sh | 30 +++++++++++---------- 2 files changed, 69 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d44d83a..313d50b 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,20 @@ licence: Free software GNU GPL 3.0 Source: https://git-repo.iml.unibe.ch/iml-open-source/onfilechange +## Why? + +You can use it to trigger actions that are not allowed by another user. + +Scenario: + +A web developer has access with a non privileged user to maintain an +application ... and on an update you want to allow him to restart the +web service without sudo permissions. + +Start the onfilechange to watch a defined file as root: + +`onfilechange.sh -f /var/www/touch2restart_httpd -c "systemctl restart httpd"` + ## Installation * Copy the bash script somewhere you like. @@ -19,24 +33,46 @@ Source: https://git-repo.iml.unibe.ch/iml-open-source/onfilechange ## Usage -``` -Parameters - -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 - -h - show this help - -i - force inotifywait command - -s - force stat command - -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 +start `./onfilechange.sh -h` to get the help ``` +______________________________________________________________________________ + + 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 +______________________________________________________________________________ + v1.04 + +INFO: stat command detected +HELP: + This script checks the change of a given file 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 + -h + show this help + -i + force inotifywait command + -s + force stat command (default mode) + -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 + +EXAMPLES: + onfilechange.sh -f /home/me/touchfile.txt -c "ls -l" + watch touchfile.txt and make a file listing on change + + onfilechange.sh -f "/home/me/touchfile.txt home/me/touchfile2.txt" -c "ls -l" + watch touchfile.txt and touchfile2.txt + + onfilechange.sh -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 +``` ## Test on command line @@ -57,7 +93,7 @@ In `/etc/systemd/system/` create a service config file **onfilechange-demo.servi ``` [Unit] -Description=Onfilechange edemo +Description=Onfilechange demo Wants=multi-user.target [Service] diff --git a/onfilechange.sh b/onfilechange.sh index 8ead4dd..724c164 100755 --- a/onfilechange.sh +++ b/onfilechange.sh @@ -19,6 +19,7 @@ # ---------------------------------------------------------------------- # 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 # ====================================================================== # ---------------------------------------------------------------------- @@ -32,7 +33,7 @@ sWatchFile= sMode= # ---- below are some internal variables -sVersion=1.03 +sVersion=1.04 # ---------------------------------------------------------------------- @@ -41,6 +42,7 @@ sVersion=1.03 # show help function showHelp(){ + local _self=$( basename $0 ) cat <<ENDOFHELP HELP: This script checks the change of a given file and triggers @@ -63,13 +65,13 @@ PRAMETERS: for stat mode: wait time in seconds betweeen each test or on missing file; default: 5 sec EXAMPLES: - `basename $0` -f /home/me/touchfile.txt -c "ls -l" + $_self -f /home/me/touchfile.txt -c "ls -l" watch touchfile.txt and make a file listing on change - `basename $0` -f "/home/me/touchfile.txt home/me/touchfile2.txt" -c "ls -l" + $_self -f "/home/me/touchfile.txt home/me/touchfile2.txt" -c "ls -l" watch touchfile.txt and touchfile2.txt - `basename $0` -f /home/me/touchfile.txt -s -w 10 -c "echo hello" + $_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 ENDOFHELP @@ -82,7 +84,7 @@ ENDOFHELP # param string text message to show function wd(){ if [ $bDebug -ne 0 ]; then - echo "[`date`] DEBUG |" $* + echo "[$(date)] DEBUG |" $* fi } @@ -110,9 +112,9 @@ function getFilestatus(){ # global (string) $TmpFile last/ initial file status # global (string) $sTmpFile2 current file status function initFilestatus(){ - getFilestatus >${sTmpFile} - wd "`cat ${sTmpFile}`" - cp -p ${sTmpFile} ${sTmpFile2} + getFilestatus >"${sTmpFile}" + wd "$(cat "${sTmpFile}")" + cp -p "${sTmpFile}" "${sTmpFile2}" } # for stat: compare file status and execute command on change @@ -122,7 +124,7 @@ function initFilestatus(){ # global (string) $sCommand command to execute function compareFilestatus(){ getFilestatus >${sTmpFile2} - wd "`cat ${sTmpFile2}`" + wd "$(cat ${sTmpFile2})" diff ${sTmpFile} ${sTmpFile2} if [ $? -eq 0 ]; then wd "No Change" @@ -147,7 +149,7 @@ function compareFilestatus(){ function execCommand(){ listFiles echo - echo ">>>>> `date` Executing ${sCommand} ..." + echo ">>>>> $(date) Executing ${sCommand} ..." ${sCommand} } @@ -262,16 +264,16 @@ echo # ---------------------------------------------------------------------- echo ">>>>> start" -myset=`echo ${sWatchFile} | sha1sum | cut -f 1 -d " "` -sTmpFile="/tmp/`basename $0`-${myset}-last.tmp" -sTmpFile2="/tmp/`basename $0`-${myset}-current.tmp" +myset=$(echo "${sWatchFile}" | sha1sum | cut -f 1 -d " ") +sTmpFile="/tmp/$(basename $0)-${myset}-last.tmp" +sTmpFile2="/tmp/$(basename $0)-${myset}-current.tmp" case $sMode in "inotifywait") while true; do listFiles >/dev/null 2>&1 if [ $? -eq 0 ]; then - inotifywait -e attrib -e modify ${sWatchFile} && execCommand + 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." -- GitLab