Skip to content

Instantly share code, notes, and snippets.

@imwower
Created February 16, 2017 07:20
Show Gist options
  • Save imwower/3510a91da38c770930255e0fbeeb9e5f to your computer and use it in GitHub Desktop.
Save imwower/3510a91da38c770930255e0fbeeb9e5f to your computer and use it in GitHub Desktop.
auto start ngrokd script on centos, put this script to /etc/init.d/ngrokd
#!/usr/bin/env bash
# These setting need you to set.
LOCATION=/usr/local/src/ngrok/bin
TLSKEY=/usr/local/src/ngrok/bin/device.key
TLSCRT=/usr/local/src/ngrok/bin/device.crt
DOMAIN=ngrok.domain.com
PORT=8000
HTTPSPORT=4443
LOGFILE=/usr/local/src/ngrok/bin/logs/ngrokd.log
PIDFILE=/usr/local/src/ngrok/bin/run/ngrokd.pid
NAME=ngrokd
# you should use /lib/init.d/functions in centos
#. /lib/lsb/init-functions
. /etc/init.d/functions
do_start()
{
if [ -s $PIDFILE ]; then
RETVAL=1
echo "$NAME already running!"
else
echo "starting $NAME"
nohup $LOCATION/$NAME -domain="$DOMAIN" -httpAddr=":$PORT" -log=$LOGFILE -log-level=INFO >/dev/null 2>&1 &
RETVAL="$?"
PID="$!"
echo $PID
if [ "$RETVAL" = "0" ]; then
echo $PID > $PIDFILE
chmod 755 $LOGFILE
echo "$NAME started."
return $RETVAL
else
echo "start $NAME failed!"
return $RETVAL
fi
fi
}
do_stop()
{
killproc -p $PIDFILE $LOCATION/$NAME
RETVAL="$?"
if [ "$RETVAL" = "0" ]; then
rm -rf $PIDFILE
echo "stop $NAME successfully."
else
echo "stop $NAME failed!"
fi
return $RETVAL
}
case "$1" in
start)
do_start
;;
stop)
echo "stopping $NAME"
do_stop
;;
status)
if [ ! -s $PIDFILE ]; then
echo "$NAME is not running."
else
PID=`cat $PIDFILE`
if [[ -n $PID && -n "`ps -p $PID | grep $PID`" ]]; then
echo "$NAME is running (${PID})"
else
echo "$NAME is not running, yet ${PIDFILE} exists (stop ngrokd will fix this)"
fi
fi
;;
*)
echo "Usage: ngrokd (start|stop|status)"
exit 3
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment