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
e77a8ad9
Commit
e77a8ad9
authored
3 years ago
by
Hahn Axel (hahn)
Browse files
Options
Downloads
Patches
Plain Diff
v1.04: shell fixes; update readme
parent
57dec42a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.md
+53
-17
53 additions, 17 deletions
README.md
onfilechange.sh
+16
-14
16 additions, 14 deletions
onfilechange.sh
with
69 additions
and
31 deletions
README.md
+
53
−
17
View file @
e77a8ad9
...
...
@@ -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
e
demo
Description=Onfilechange demo
Wants=multi-user.target
[Service]
...
...
This diff is collapsed.
Click to expand it.
onfilechange.sh
+
16
−
14
View file @
e77a8ad9
...
...
@@ -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.0
3
sVersion
=
1.0
4
# ----------------------------------------------------------------------
...
...
@@ -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."
...
...
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