Skip to content

Instantly share code, notes, and snippets.

@GusAntoniassi
Created March 29, 2020 12:33
Show Gist options
  • Save GusAntoniassi/155fbe0cf74a6d794174d62d0f6a0156 to your computer and use it in GitHub Desktop.
Save GusAntoniassi/155fbe0cf74a6d794174d62d0f6a0156 to your computer and use it in GitHub Desktop.
Inotifywait Test

This was an attempt of making a live-reload system with Golang and inotifywait.

In the end it didn't work out because the application port (3000) would not be released until the parent proccess associated with it terminated.

So kill killed the golang proccess, but the inotify.sh proccess was still running and the port wasn't released, giving a "Port already in use" error when the new proccess started.

I have dropped this in favor of modd, but this might be useful for something one day.

#!/usr/bin/env bash
pid=""
function monitor() {
inotifywait --exclude '\.swp|.*~$' --event close_write --recursive . |
while read -r directory events filename; do
if ! echo "$directory" | grep -Eq '^\.\/\.git' &&
! git check-ignore --non-matching --verbose "${directory}/${filename}" >/dev/null 2>&1; then
if [ "${pid}" ]; then
echo "Killing old proccess ($pid)"
kill "${pid}"
tail --pid="${pid}" -f /dev/null
fi
echo "Files changed, restarting server"
go run main.go &
pid="$!"
echo "Started proccess with PID ${pid}"
monitor
fi
done
}
cd src/api
monitor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment