Skip to content

Instantly share code, notes, and snippets.

@krpors
Last active May 30, 2020 08:30
Show Gist options
  • Save krpors/c334ea2a3428d6ff19cdcb2ce458822c to your computer and use it in GitHub Desktop.
Save krpors/c334ea2a3428d6ff19cdcb2ce458822c to your computer and use it in GitHub Desktop.
inotifywait for cargo
#!/usr/bin/env bash
# Clear the screen, run the initial command in the background immediately,
# and then get the PID. We need that to kill it later (if the program is a
# server/daemon or the like).
${*} &
pid=$!
# Now run inotifywait and only act on certain events, recursively.
# Kill the previous PID (if applicable). Will output an error if the
# PID cannot be found but that can be ignored.
inotifywait -m -q -e CLOSE_WRITE,DELETE,MOVED_FROM -r . |
while read path action file; do
if [[ "${file}" =~ .rs$ ]]; then
printf "\e[33;1m================================================\n"
printf "\e[32;1mRunning \e[0m'${*}'\n"
printf "\e[32;1mKilling \e[0mprevious PID ${pid}...\n"
printf "\e[33;1m================================================\n"
${*} &
pid=$!
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment