Skip to content

Instantly share code, notes, and snippets.

@smoser
Created April 27, 2016 18:21
Show Gist options
  • Save smoser/54df53cef0255acef82e8fb0d62c4dfc to your computer and use it in GitHub Desktop.
Save smoser/54df53cef0255acef82e8fb0d62c4dfc to your computer and use it in GitHub Desktop.
inotifywait example
#!/bin/bash
# just an example using inotifywait. the 'process' function basically reads from
# the output of inotifywait and then when it is found what it is after it will kill the
# inotifywait process.
#
# used this to hack a dpkg postinst script that wasnt yet installed and enable some debugging
# in it before it was run.
LAUNCH_PID=""
LOG="/run/${0##*/}.log"
set -f
cleanup() {
if [ -n "$LAUNCH_PID" -a -e "/proc/$LAUNCH_PID" ]; then
echo "$$ killing $LAUNCH_PID"
kill "$LAUNCH_PID"
fi
}
launch() {
echo $$
#exec inotifywait --monitor --recursive --event=modify,create /var/lib/dpkg/info
exec inotifywait --monitor --recursive /var/lib/dpkg/info
}
process() {
echo "process: pid=$$"
read LAUNCH_PID
echo "process: launch_pid=$LAUNCH_PID"
while :; do
read dir op file other
ret=$?
[ $ret -eq 0 ] || { echo "read failed: $ret"; exit $ret; }
echo "ret=$ret dir=$dir op=$op file=$file${other:+ other=${other}}"
fpath="$dir$file"
if [ "$file" = "apache2.postinst" ]; then
echo "modifying $fpath"
sed -i -e 's,set -e$,set -ex,' "$fpath"
ret=$?
echo "ret=$? fixed $fpath"
exit $ret
fi
done
}
if [ "$1" = "process" ]; then
process
elif [ "$1" = "launch" ]; then
launch
else
trap cleanup EXIT
exec >"$LOG" 2>&1
echo "parent $$"
"$0" launch | process
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment