Skip to content

Instantly share code, notes, and snippets.

@bergkvist
Created September 4, 2022 21:25
Show Gist options
  • Save bergkvist/c8c40ceb8c8365406cb94b11e880550c to your computer and use it in GitHub Desktop.
Save bergkvist/c8c40ceb8c8365406cb94b11e880550c to your computer and use it in GitHub Desktop.
Watch a file for changes using inotifywait and kill previous process if still running.
#!/bin/sh
build_and_run() {
cc main.c -o main -Wall -Wextra
./main
}
trap 'kill "$p" >/dev/null 2>/dev/null' INT
build_and_run &
p=$!
inotifywait main.c -q -m -e CLOSE_WRITE |
while read -r; do
kill "$p" >/dev/null 2>/dev/null
trap 'kill "$p" >/dev/null 2>/dev/null' INT
build_and_run &
p=$!
done
@bergkvist
Copy link
Author

Alternative using entr(1) (which also clears terminal on restart):

ls main.c | entr -rcs "cc main.c -o main -Wall -Wextra && ./main"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment