I'm trying to write a script which restarts a python3 -m http.server
process when a certain directory (_site
) is deleted and then recreated. The script is below. There's an inotify command in the waitdel
function which is supposed to only block until the directory is deleted. When it is deleted, the execution goes on with a simple polling wait until the directory is created, then the server restarts, and finally we're back to waiting.
The trouble is, when _site
is deleted, inotifywait
never exits in the shell script, even tho the exact same command does exit when I run it at the very same bash prompt I run this script in, both on DELETE
and DELETE_SELF
.
I've verified that the correct inotifywait
command is run, and that the server process is not blocking the execution of the script. So, why is it not exiting in the script?
#!/usr/bin/env bash
# serve.bash --- serve content, respawn server when _site/ is deleted
# bash strict mode
set -euo pipefail
IFS=$'\n\t'
PID=0
DIR="$PWD/_site" # NO FOLLOWING SLASH OR BREAKS INOTIFYWAIT
PIDFILE="$PWD/.server.pid"
die () {
echo $0: error: $@
exit 2
}
say () {
echo $0 \[$(date)\]: $@
}
serve () {
cleanup
old="$PWD"
cd "$DIR" || die Could not cd to "$DIR"
python3 -m http.server 8000 --bind 127.0.0.1 2>&1 &
echo $! > "$PIDFILE"
cd "$old"
}
waitdel () {
while true; do
say Set up watcher for "$DIR"...
inotifywait -e delete_self "$DIR"
say "$DIR" deleted, restarting server...
# Wait&poll till the directory is recreated.
while [ ! -e "$DIR" ]; do
sleep 0.1
done
serve
done
}
cleanup () {
if [[ ! -e "$PIDFILE" ]]; then
return
fi
sync
PID="$(cat $PIDFILE)" && rm "$PIDFILE"
say Kill pid="$PID"...
[ "0" = "$PID" ] || kill -9 "$PID" \
|| die Failed to kill preexisting server on pid "$PID"
}
trap cleanup SIGINT SIGTERM EXIT
if [ -e "$PIDFILE" ]; then
if pgrep -a python3 | grep http\\.server >/dev/null; then
trap - SIGINT SIGTERM EXIT
die Stale pidfile found at "$PIDFILE", a potentially orphaned \
server might be running. Please kill it before proceeding.
else
rm "$PIDFILE" # remove stale pidfile when no server proc found
fi
fi
serve
waitdel