Skip to content

Instantly share code, notes, and snippets.

@cstrahan
Last active April 26, 2016 17:21
Show Gist options
  • Save cstrahan/0403676f98980c5813b1292859104db0 to your computer and use it in GitHub Desktop.
Save cstrahan/0403676f98980c5813b1292859104db0 to your computer and use it in GitHub Desktop.
auto build/run example based on watchman-make

#Usage

Install watchman, and then run ./autobuild.

Modify restart such that it builds your project appropriately, and modify the ENV=some_program line to instead be whatever program you wanted to run.

#!/bin/sh
watchman-make --make './restart' -p '**/*.go' -t ignored
package main
import (
"fmt"
)
func main() {
fmt.Println("HELLO WORLD!")
}
#!/bin/sh
EXE=./some_program
PIDFILE=pid/service.pid
msg() {
echo -e "\e[92m$@\e[0m"
}
# kill existing process
if [ -f $PIDFILE ]; then
pid=$(cat $PIDFILE)
msg "killing previous process ($pid) . . ."
kill $pid >/dev/null 2>&1
# wait for it to die
while kill -0 $pid >/dev/null 2>&1; do
msg "waiting for process to exit . . ."
sleep 0.5
done
fi
msg "building project . . ."
go build -o some_program main.go
msg "starting new process . . ."
# fork, so watchman-make doesn't hang.
exec $EXE &
# write the new PIDFILE
mkdir -p $(dirname $PIDFILE)
echo $! > $PIDFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment