Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
onfilechange
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
onfilechange
Commits
64ce8bc9
Commit
64ce8bc9
authored
2 years ago
by
Hahn Axel (hahn)
Browse files
Options
Downloads
Patches
Plain Diff
fix: behaviur when trigger command fails
parent
21eea8f0
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+2
-2
2 additions, 2 deletions
README.md
docs/_index.md
+2
-2
2 additions, 2 deletions
docs/_index.md
onfilechange.sh
+32
-37
32 additions, 37 deletions
onfilechange.sh
with
36 additions
and
41 deletions
README.md
+
2
−
2
View file @
64ce8bc9
# onfilechange
# onfilechange
A Shell script that watches a given file or multiple file
s.
A Shell script that watches a given file
object
or multiple file
objects (=files or directories).
If one of the watched file changes then a given command will be exxecuted.
If one of the watched file
object
changes then a given command will be exxecuted.
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.
...
...
This diff is collapsed.
Click to expand it.
docs/_index.md
+
2
−
2
View file @
64ce8bc9
# onfilechange
# onfilechange
A Shell script that watches a given file or multiple file
s.
A Shell script that watches a given file
object
or multiple file
objects (=files or directories).
If one of the watched file changes then a given command will be exxecuted.
If one of the watched file
object
changes then a given command will be exxecuted.
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.
...
...
This diff is collapsed.
Click to expand it.
onfilechange.sh
+
32
−
37
View file @
64ce8bc9
...
@@ -5,8 +5,9 @@
...
@@ -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
# 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.
# A Shell script that watches a given fileobject or multiple fileobjects
# If the file changes then a given command will be exxecuted.
# (=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
# 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.
#
#
...
@@ -15,11 +16,13 @@
...
@@ -15,11 +16,13 @@
#
#
# 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
# docs: https://os-docs.iml.unibe.ch/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
# 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=
...
@@ -33,7 +36,7 @@ sWatchFile=
sMode
=
sMode
=
# ---- below are some internal variables
# ---- below are some internal variables
sV
ersion
=
1.0
4
_v
ersion
=
1.0
5
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
...
@@ -45,14 +48,15 @@ function showHelp(){
...
@@ -45,14 +48,15 @@ function showHelp(){
local
_self
=
$(
basename
$0
)
local
_self
=
$(
basename
$0
)
cat
<<
ENDOFHELP
cat
<<
ENDOFHELP
HELP:
HELP:
This script checks the change of a given file and triggers
This script checks the change of a given file
objects
and triggers
a command if it changes
a command if it changes
PRAMETERS:
PRAMETERS:
-c [command]
-c [command]
command to execute on a file change
command to execute on a file change
-f [filename(s)]
-f [fileobject(s)]
filenames to watch; separate multiple files with space and put all in quotes
filenames or directories to watch; separate multiple files with
space and put all in quotes
-h
-h
show this help
show this help
-i
-i
...
@@ -62,7 +66,8 @@ PRAMETERS:
...
@@ -62,7 +66,8 @@ PRAMETERS:
-v
-v
verbose mode; enable showing debug output
verbose mode; enable showing debug output
-w [integer]
-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:
EXAMPLES:
$_self
-f /home/me/touchfile.txt -c "ls -l"
$_self
-f /home/me/touchfile.txt -c "ls -l"
...
@@ -72,7 +77,8 @@ EXAMPLES:
...
@@ -72,7 +77,8 @@ EXAMPLES:
watch touchfile.txt and touchfile2.txt
watch touchfile.txt and touchfile2.txt
$_self
-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
watch touchfile.txt every 10 sec with stat and show "hello" on a
change
ENDOFHELP
ENDOFHELP
...
@@ -125,21 +131,19 @@ function initFilestatus(){
...
@@ -125,21 +131,19 @@ function initFilestatus(){
function
compareFilestatus
(){
function
compareFilestatus
(){
getFilestatus
>
${
sTmpFile2
}
getFilestatus
>
${
sTmpFile2
}
wd
"
$(
cat
${
sTmpFile2
}
)
"
wd
"
$(
cat
${
sTmpFile2
}
)
"
diff
${
sTmpFile
}
${
sTmpFile2
}
if
diff
${
sTmpFile
}
${
sTmpFile2
}
;
then
if
[
$?
-eq
0
]
;
then
wd
"No Change"
wd
"No Change"
else
else
wd
"Change detected."
wd
"Change detected."
execCommand
if
execCommand
;
then
if
[
$?
-eq
0
]
;
then
echo
Command was successful.
echo
Command was successful.
echo
wd
"Re-Init File status"
# initFilestatus
mv
${
sTmpFile2
}
${
sTmpFile
}
else
else
echo
FAILED.
echo
rc
=
$?
FAILED.
fi
fi
wd
"Re-Init File status"
mv
${
sTmpFile2
}
${
sTmpFile
}
echo
echo
">>>>> waiting for the next change ..."
fi
fi
}
}
...
@@ -147,8 +151,6 @@ function compareFilestatus(){
...
@@ -147,8 +151,6 @@ function compareFilestatus(){
#
#
# global (string) $sCommand command line to exectute
# global (string) $sCommand command line to exectute
function
execCommand
(){
function
execCommand
(){
listFiles
echo
echo
">>>>>
$(
date
)
Executing
${
sCommand
}
..."
echo
">>>>>
$(
date
)
Executing
${
sCommand
}
..."
${
sCommand
}
${
sCommand
}
}
}
...
@@ -160,25 +162,22 @@ function execCommand(){
...
@@ -160,25 +162,22 @@ function execCommand(){
cat
<<
ENDOFHEAD
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
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
}
v
${
sVersion
}
ENDOFHEAD
ENDOFHEAD
which
stat
>
/dev/null 2>&1
if
which
stat
>
/dev/null 2>&1
;
then
if
[
$?
-ne
0
]
;
then
echo
"INFO: stat command detected"
sMode
=
stat
else
echo
ERROR: the
command stat
was not found on your system.
echo
ERROR: the
command stat
was not found on your system.
which inotifywait
>
/dev/null 2>&1
if
!
which inotifywait
>
/dev/null 2>&1
;
then
if
[
$?
-ne
0
]
;
then
echo
ERROR: the
command
inotifywait 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 inotifywait command"
echo
"INFO: enabling inotifywait command"
sMode
=
inotifywait
sMode
=
inotifywait
else
echo
"INFO: stat command detected"
sMode
=
stat
fi
fi
if
[
$#
-eq
0
]
;
then
if
[
$#
-eq
0
]
;
then
...
@@ -189,7 +188,7 @@ fi
...
@@ -189,7 +188,7 @@ fi
while
getopts
":c: :v :f: :h :i :s :w:"
opt
while
getopts
":c: :v :f: :h :i :s :w:"
opt
do
do
case
$opt
in
case
$opt
in
\?
|
h
)
h
)
showHelp
showHelp
exit
0
exit
0
;;
;;
...
@@ -246,8 +245,7 @@ if [ -z "${sWatchFile}" ]; then
...
@@ -246,8 +245,7 @@ if [ -z "${sWatchFile}" ]; then
echo
ERROR:
set
a check file with param
-f
echo
ERROR:
set
a check file with param
-f
exit
1
exit
1
fi
fi
listFiles
if
!
listFiles
;
then
if
[
$?
-ne
0
]
;
then
echo
"INFO: file
${
sWatchFile
}
(or one of them) does not exist yet"
echo
"INFO: file
${
sWatchFile
}
(or one of them) does not exist yet"
# echo "ERROR: file ${sWatchFile} (or one of them) does not exist yet"
# echo "ERROR: file ${sWatchFile} (or one of them) does not exist yet"
# exit 1
# exit 1
...
@@ -271,14 +269,11 @@ sTmpFile2="/tmp/$(basename $0)-${myset}-current.tmp"
...
@@ -271,14 +269,11 @@ sTmpFile2="/tmp/$(basename $0)-${myset}-current.tmp"
case
$sMode
in
case
$sMode
in
"inotifywait"
)
"inotifywait"
)
while
true
;
do
while
true
;
do
listFiles
>
/dev/null 2>&1
if
listFiles
>
/dev/null 2>&1
;
then
if
[
$?
-eq
0
]
;
then
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 stat 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
}
sleep
${
iSleep
}
exit
2
exit
2
fi
fi
done
done
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment