-
-
Save yarick123/5405365 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/sh | |
##3.5...10...15...20...25...30...35...40...45...50...55...60...65...70...75...80 | |
## | |
## Debian / Linux / Ubuntu / LSB | |
## Startup script for Express / Node.js application with the forever module | |
## | |
## | |
## A modification of "init.d.lsb.ex" by Nicolas Thouvenin | |
## | |
## | |
## This is free software; you may redistribute it and/or modify | |
## it under the terms of the GNU General Public License as | |
## published by the Free Software Foundation; either version 2, | |
## or (at your option) any later version. | |
## | |
## This is distributed in the hope that it will be useful, but | |
## WITHOUT ANY WARRANTY; without even the implied warranty of | |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
## GNU General Public License for more details. | |
## | |
## You should have received a copy of the GNU General Public License with | |
## the Debian operating system, in /usr/share/common-licenses/GPL; if | |
## not, write to the Free Software Foundation, Inc., 59 Temple Place, | |
## Suite 330, Boston, MA 02111-1307 USA | |
## | |
## | |
################################################################################ | |
################################################################################ | |
## ## | |
# APPLICATION section # | |
## ## | |
################################################################################ | |
################################################################################ | |
# !!! | |
# !!! If you do not want to change the variables below. | |
# !!! You must, first, rename the script with the name of the directory created | |
# !!! by "express" and add the suffix ".sh" | |
# !!! Second, you must place this script right next to the directory created | |
# !!! by "express" | |
# !!! | |
# !!! For example: | |
# !!! /tmp/foo # Created by the command "express /tmp/foo" | |
# !!! /tmp/foo.sh # This script | |
# !!! | |
DESC=`/usr/bin/basename $0 .sh` | |
NAME="app.js" | |
START_PARAMS="" | |
DIR=`/usr/bin/dirname $(/bin/readlink -f $0)` | |
APP=${DIR}/${DESC}/${NAME} | |
export NODE_ENV=${NODE_ENV:="production"} | |
################################################################################ | |
################################################################################ | |
## ## | |
# PATHs section # | |
## ## | |
################################################################################ | |
################################################################################ | |
export PATH=$HOME/local/bin:${PATH:=} | |
export MANPATH=$HOME/local/man:${MANPATH:=} | |
export LD_LIBRARY_PATH=$HOME/local/lib:${LD_LIBRARY_PATH:=} | |
################################################################################ | |
################################################################################ | |
## ## | |
# FOREVER section # | |
## ## | |
################################################################################ | |
################################################################################ | |
running() { | |
forever list 2>/dev/null | grep ${APP} 2>&1 >/dev/null | |
return $? | |
} | |
start_server() { | |
forever start ${APP} ${START_PARAMS} 2>&1 >/dev/null | |
return $? | |
} | |
stop_server() { | |
forever stop ${APP} 2>&1 >/dev/null | |
return $? | |
} | |
################################################################################ | |
################################################################################ | |
## ## | |
# GENERIC section # | |
## ## | |
################################################################################ | |
################################################################################ | |
. /lib/lsb/init-functions | |
DIETIME=10 # Time to wait for the server to die, in seconds | |
# If this value is set too low you might not | |
# let some servers to die gracefully and | |
# 'restart' will not work | |
STARTTIME=2 # Time to wait for the server to start, in seconds | |
# If this value is set each time the server is | |
# started (on start or restart) the script will | |
# stall to try to determine if it is running | |
# If it is not set and the server takes time | |
# to setup a pid file the log message might | |
# be a false positive (says it did not start | |
# when it actually did) | |
case "$1" in | |
start) | |
log_daemon_msg "Starting $DESC " "$NAME" | |
# Check if it's running first | |
if running ; then | |
log_progress_msg "apparently already running" | |
log_end_msg 0 | |
exit 0 | |
fi | |
if start_server ; then | |
# NOTE: Some servers might die some time after they start, | |
# this code will detect this issue if STARTTIME is set | |
# to a reasonable value | |
[ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time | |
if running ; then | |
# It's ok, the server started and is running | |
log_end_msg 0 | |
else | |
# It is not running after we did start | |
log_end_msg 1 | |
fi | |
else | |
# Either we could not start it | |
log_end_msg 1 | |
fi | |
;; | |
stop) | |
log_daemon_msg "Stopping $DESC" "$NAME" | |
if running ; then | |
# Only stop the server if we see it running | |
errcode=0 | |
stop_server || errcode=$? | |
log_end_msg $errcode | |
else | |
# If it's not running don't do anything | |
log_progress_msg "apparently not running" | |
log_end_msg 0 | |
exit 0 | |
fi | |
;; | |
restart) | |
log_daemon_msg "Restarting $DESC" "$NAME" | |
errcode=0 | |
stop_server || errcode=$? | |
# Wait some sensible amount, some server need this | |
[ -n "$DIETIME" ] && sleep $DIETIME | |
start_server || errcode=$? | |
[ -n "$STARTTIME" ] && sleep $STARTTIME | |
running || errcode=$? | |
log_end_msg $errcode | |
;; | |
status) | |
log_daemon_msg "Checking status of $DESC" "$NAME" | |
if running ; then | |
log_progress_msg "running" | |
log_end_msg 0 | |
else | |
log_progress_msg "apparently not running" | |
log_end_msg 1 | |
exit 1 | |
fi | |
;; | |
*) | |
echo "Usage: ${0} {start|stop|status|restart}" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment