Skip to content

Instantly share code, notes, and snippets.

@xpl
Last active October 22, 2021 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpl/6ecebf02fd21729e35f57496f755480c to your computer and use it in GitHub Desktop.
Save xpl/6ecebf02fd21729e35f57496f755480c to your computer and use it in GitHub Desktop.
Restart a binary upon change (bash script)
#!/bin/bash
# ---------------------------------------
# EXAMPLE USE:
#
# ./watch.sh target/debug/my-server-app
# ---------------------------------------
kill_prev_pid () {
echo Killing prev pid $(cat .pid)
kill $(cat .pid) 2>/dev/null
}
kill_and_exit () {
echo Exiting watch...
kill_prev_pid
exit 0
}
trap kill_and_exit 1 2 3 6
while :
do
kill_prev_pid
$1 &
PID=$!
echo New pid $PID
echo $PID > .pid
inotifywait $1
echo Executable file changed - restarting pid $PID
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment