Last active
October 22, 2021 17:39
-
-
Save xpl/6ecebf02fd21729e35f57496f755480c to your computer and use it in GitHub Desktop.
Restart a binary upon change (bash script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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