-
-
Save petruisfan/14f85d4264557602fdd6 to your computer and use it in GitHub Desktop.
Node init.d service script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
############### | |
# SysV Init Information | |
# chkconfig: - 58 74 | |
# description: node daemon. | |
### BEGIN INIT INFO | |
# Provides: node | |
# Required-Start: | |
# Required-Stop: | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: manage node daemon | |
# Description: node daemon | |
### END INIT INFO | |
HOME=/opt/node | |
PATH=$PATH:/usr/local/bin | |
EXEC=/usr/local/bin/supervisor | |
PIDFILE=/var/run/node.pid | |
NAME="node" | |
LOG_SRC=/var/log/node | |
[ ! -d "${LOG_SRC}" ] && mkdir -p "${LOG_SRC}" | |
case "$1" in | |
start) | |
# | |
# Check if port is free | |
# | |
PORT_STATUS=`netstat -lnt | awk '$6 == "LISTEN" && $4 ~ ".3000"'` | |
if [[ -n ${PORT_STATUS} ]]; then | |
echo "Port ${PORT} is in use. Not starting" | |
exit 5; | |
fi | |
if [ -f ${PIDFILE} ] | |
then | |
PID=$(cat ${PIDFILE}) | |
PS=`ps -Fp ${PID} | wc -l` | |
if [ ${PS} -gt 1 ]; then | |
echo "$PIDFILE exists, process is running" | |
exit 1; | |
else | |
echo "$PIDFILE exists, removing it." | |
rm ${PIDFILE} | |
fi | |
fi | |
echo "Starting $NAME server..." | |
cd $HOME | |
nohup ${EXEC} -pid ${PIDFILE} index.js &> ${LOG_SRC}/error.log < /dev/null & | |
# echo $! > ${PIDFILE} | |
;; | |
stop) | |
if [ ! -f ${PIDFILE} ] | |
then | |
echo "$PIDFILE does not exist, process is not running" | |
else | |
PID=$(cat $PIDFILE) | |
echo "Stopping $PID..." | |
PS=`ps -Fp ${PID} | wc -l` | |
if [ 1 -gt ${PS} ]; then | |
echo "No such process" | |
rm ${PIDFILE} | |
exit 4; | |
fi | |
kill ${PID} | |
while [ -x /proc/${PID} ] | |
do | |
PS=`ps -Fp ${PID} | wc -l` | |
if [ 1 -gt ${PS} ]; then | |
rm ${PIDFILE} | |
fi | |
echo "Waiting for $NAME to shutdown ..." | |
sleep 1 | |
done | |
# | |
# No pid, so just remove the file | |
# | |
[[ -f ${PIDFILE} ]] && rm ${PIDFILE} | |
echo "$NAME stopped" | |
fi | |
;; | |
status) | |
if [ ! -f ${PIDFILE} ] | |
then | |
echo "$NAME is not running" | |
else | |
echo "$NAME is running ($(<${PIDFILE}))" | |
fi | |
;; | |
restart) | |
$0 stop | |
$0 start | |
;; | |
*) | |
echo "Please use start, stop, restart or status as first argument" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment