Skip to content

Instantly share code, notes, and snippets.

@ikennaokpala
Last active July 25, 2024 03:38
Show Gist options
  • Save ikennaokpala/8dcbaf7c2fe0da9b2a65 to your computer and use it in GitHub Desktop.
Save ikennaokpala/8dcbaf7c2fe0da9b2a65 to your computer and use it in GitHub Desktop.
Starting up and Monitoring a Golang binary in production
# Make it executable
sudo chmod +x etc/init.d/myapp
# Try it:
sudo service myapp start
# Make it run upon boot:
sudo update-rc.d myapp defaults
#! /bin/sh
# /etc/init.d/myapp
#
USER="user"
GOPATH="/path/to/apps/go"
GOROOT="$GOPATH"
WORKDIR="$GOPATH/bin/"
# Carry out specific functions when asked to by the system
case "$1" in
start)
cd "$WORKDIR"
echo "Start Golang script"
export GOPATH="$GOPATH"
export PATH="${PATH}:${GOROOT}/bin:${GOPATH}/bin"
#/bin/su -m -l $USER -c "nohup ./myapp > $WORKDIR/myapp.log 2>&1 & echo $! > /path/to/apps/pids/myapp.pid"
/bin/su -m -l $USER -c "nohup ./myapp > /dev/null 2>&1 & echo $! > /path/to/apps/pids/myapp.pid"
;;
stop)
echo "Stop Golang script"
kill -9 `cat /path/to/apps/pids/myapp.pid`
;;
*)
echo "Usage: /etc/init.d/myapp {start|stop}"
exit 1
;;
esac
exit 0
http://wiki.colar.net/golang_revel_init_script#.VIOVo4vufdk
#! /bin/bash
# Go/Revel server init script - Thibaut Colar.
### BEGIN INIT INFO
# Provides: gosway
# Required-Start: $network
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: gosway
### END INIT INFO
USER="ubuntu" # User we wil run Go as
GOPATH="/data/go" # GOPATH
GOROOT="/data/go/go" # used by revel
GO_CMD="revel run gosway" # here I use revel but could just use "go"
WORKDIR="/data/go/src/gosway"
NAME="gosway" # app name for ogs etc ...
###### Start script ########################################################
recursiveKill() { # Recursively kill a process and all subprocesses
CPIDS=$(pgrep -P $1);
for PID in $CPIDS
do
recursiveKill $PID
done
sleep 3 && kill -9 $1 2>/dev/null & # hard kill after 3 seconds
kill $1 2>/dev/null # try soft kill first
}
case "$1" in
start)
echo "Starting $NAME ..."
if [ -f "$WORKDIR/$NAME.pid" ]
then
echo "Already running according to $WORKDIR/$NAME.pid"
exit 1
fi
cd "$WORKDIR"
export GOROOT="$GOROOT"
export GOPATH="$GOPATH"
export PATH="${PATH}:${GOROOT}/bin:${GOPATH}/bin"
/bin/su -m -l $USER -c "$GO_CMD" > "$WORKDIR/$NAME.log" 2>&1 &
PID=$!
echo $PID > "$WORKDIR/$NAME.pid"
echo "Started with pid $PID - Logging to $WORKDIR/$NAME.log" && exit 0
;;
stop)
echo "Stopping $NAME ..."
if [ ! -f "$WORKDIR/$NAME.pid" ]
then
echo "Already stopped!"
exit 1
fi
PID=`cat "$WORKDIR/$NAME.pid"`
recursiveKill $PID
rm -f "$WORKDIR/$NAME.pid"
echo "stopped $NAME" && exit 0
;;
restart)
$0 stop
sleep 1
$0 start
;;
status)
if [ -f "$WORKDIR/$NAME.pid" ]
then
PID=`cat "$WORKDIR/$NAME.pid"`
if [ "$(/bin/ps --no-headers -p $PID)" ]
then
echo "$NAME is running (pid : $PID)" && exit 0
else
echo "Pid $PID found in $WORKDIR/$NAME.pid, but not running." && exit 1
fi
else
echo "$NAME is NOT running" && exit 1
fi
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" && exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment