Utoljára aktív 6 months ago

node_debian_init.sh Eredeti
1#!/bin/bash
2# ------------------------------------------------------------------------------
3# SOME INFOS : fairly standard (debian) init script.
4# Note that node doesn't create a PID file (hence --make-pidfile)
5# has to be run in the background (hence --background)
6# and NOT as root (hence --chuid)
7#
8# MORE INFOS : INIT SCRIPT http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit
9# INIT-INFO RULES http://wiki.debian.org/LSBInitScripts
10# INSTALL/REMOVE http://www.debian-administration.org/articles/28
11# ------------------------------------------------------------------------------
12# #
13# BEGIN <MODIFY TO REFLECT YOUR SETTINGS> #
14# #
15#
16# 1) Don't forget to also modify the COMMENTED fields in the "### BEGIN INIT INFO"
17# below (don't uncomment them) if you wish to install system-wide with update-rc.d
18# eg: provides, Short-Description, Description
19#
20# 2) in case you have different node.js servers running, each init.d script should
21# (obviously) have a UNIQUE BASE name so that PIDS do not conflict
22# --> name them accordingly
23# eg: node_static_server, node_express1, node_load_balancer.sh...
24#
25# 3) copy the renamed/modified script(s) to /etc/init.d
26# chmod 755,
27#
28# 4) if you wish the Daemon to be lauched at boot / stopped at shutdown :
29# INSTALL : update-rc.d scriptname defaults
30# (UNINSTALL : update-rc.d -f scriptname remove)
31#
32# 5) based on : Debian /etc/init.d/skeleton
33# modified by : Peter Host (www.oghme.com)
34# modified by : Stefan Greiner (www.cadenas.de)
35# ______________________________________________________________________________
36### BEGIN INIT INFO
37# Provides: node_debian_init
38# Required-Start: $remote_fs $named $syslog
39# Required-Stop: $remote_fs $named $syslog
40# Default-Start: 2 3 4 5
41# Default-Stop: 0 1 6
42# Short-Description: DEBIAN initscript for node.js servers/apps
43# Description: DEBIAN initscript for node.js servers/apps
44# Place this file in /etc/init.d.
45### END INIT INFO
46
47# Author: My Name <myname@domain.tld>
48#
49# Please remove the "Author" lines above and replace them
50# with your own name if you copy and modify this script.
51# ______________________________________________________________________________
52#
53# PATH should only include /usr/* if it runs after the mountnfs.sh script
54PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin # modify if you need
55
56LOCAL_VAR_LOGS_DIR=/usr/local/var/log
57LOCAL_VAR_LOGS=$LOCAL_VAR_LOGS_DIR/nodejs.log
58DAEMON_ARGS="/path/to/your/app.js" # path to your node.js server/app
59 # NB: don't use ~/ in path
60
61DESC="Node.js server" # whatever fancy description you like
62
63NODEUSER=nodeuser:nodegroup # USER who OWNS the daemon process (no matter whoever runs the init script)
64 # user:group (if no group is specified, the primary GID for that user is used)
65
66LOCAL_VAR_RUN=/usr/local/var/run # in case the init script is run by non-root user, you need to
67 # indicate a directory writeable by $NODEUSER to store the PID file
68 # NB : 1) /usr/local/var/run does not exist by DEFAULT. Either create it
69 # or choose one of your own liking.
70 # 2) node, npm,... are best NOT installed/run as ROOT.
71 # (see here: https://github.com/isaacs/npm/blob/master/README.md)
72
73NAME=node # name of the node.js executable
74DAEMON=/usr/local/bin/$NAME # this SHOULD POINT TO where your node executable is
75#
76# #
77# END </MODIFY TO REFLECT YOUR SETTINGS> #
78# (Nothing else to modify from this point on...) #
79# ------------------------------------------------------------------------------
80
81[ ! -d "$LOCAL_VAR_LOGS_DIR" ] || { mkdir "$LOCAL_VAR_LOGS_DIR" -p; }
82
83# Do NOT "set -e"
84
85[ $UID -eq "0" ] && LOCAL_VAR_RUN=/var/run # in case this script is run by root, override user setting
86THIS_ARG=$0
87INIT_SCRIPT_NAME=`basename $THIS_ARG`
88#[ -h $THIS_ARG ] && INIT_SCRIPT_NAME=`basename $(readlink $THIS_ARG)` # in case of symlink
89INIT_SCRIPT_NAME_NOEXT=${INIT_SCRIPT_NAME%.*}
90PIDFILE="$LOCAL_VAR_RUN/$INIT_SCRIPT_NAME_NOEXT.pid"
91SCRIPTNAME=/etc/init.d/$INIT_SCRIPT_NAME
92
93# Read configuration variable file if it is present
94[ -r /etc/default/$INIT_SCRIPT_NAME ] && . /etc/default/$INIT_SCRIPT_NAME
95
96# Exit if the package is not installed
97[ -x "$DAEMON" ] || { echo "can't find Node.js ($DAEMON)" >&2; exit 0; }
98
99# Exit if the 'run' folder is not present
100[ -d "$LOCAL_VAR_RUN" ] || { echo "Directory $LOCAL_VAR_RUN does not exist. Modify the '$INIT_SCRIPT_NAME_NOEXT' init.d script ($THIS_ARG) accordingly" >&2; exit 0; }
101
102# Load the VERBOSE setting and other rcS variables
103. /lib/init/vars.sh
104
105# Define LSB log_* functions.
106# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
107. /lib/lsb/init-functions
108
109# uncomment to override system setting
110#VERBOSE=yes
111
112# define this after reading default values
113NODECMD="exec $DAEMON $DAEMON_ARGS >>$LOCAL_VAR_LOGS 2>&1" # node comman
114
115#
116# Function that starts the daemon/service
117#
118do_start()
119{
120 # Return
121 # 0 if daemon has been started
122 # 1 if daemon was already running
123 # 2 if daemon could not be started
124 start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $NODEUSER --background --exec $DAEMON --test > /dev/null \
125 || { [ "$VERBOSE" != no ] && log_progress_msg "---> Daemon already running.."; return 1; }
126 start-stop-daemon --start --quiet --chuid $NODEUSER --make-pidfile --pidfile $PIDFILE --background --exec $DAEMON --startas /bin/sh -- -c "$NODECMD" \
127 || { [ "$VERBOSE" != no ] && log_progress_msg "---> could not be started"; return 2; }
128 # Add code here, if necessary, that waits for the process to be ready
129 # to handle requests from services started subsequently which depend
130 # on this one. As a last resort, sleep for some time.
131 [ "$VERBOSE" != no ] && log_progress_msg "---> started"
132 return 0
133}
134
135#
136# Function that stops the daemon/service
137#
138do_stop()
139{
140 # Return
141 # 0 if daemon has been stopped
142 # 1 if daemon was already stopped
143 # 2 if daemon could not be stopped
144 # other if a failure occurred
145 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --chuid $NODEUSER --exec $DAEMON
146 RETVAL="$?"
147 #[ "$VERBOSE" != no ] && [ "$RETVAL" = 1 ] && log_daemon_msg " ---> SIGKILL failed => hardkill $DESC" "$INIT_SCRIPT_NAME_NOEXT"
148 [ "$RETVAL" = 2 ] && return 2
149 # Wait for children to finish too if this is a daemon that forks
150 # and if the daemon is only ever run from this initscript.
151 # If the above conditions are not satisfied then add some other code
152 # that waits for the process to drop all resources that could be
153 # needed by services started subsequently. A last resort is to
154 # sleep for some time.
155 start-stop-daemon --stop --quiet --oknodo --retry=0/3/KILL/5 --pidfile $PIDFILE --chuid $NODEUSER --exec $DAEMON -- $DAEMON_ARGS
156 [ "$?" = 2 ] && return 2
157 # Many daemons don't delete their pidfiles when they exit.
158 rm -f $PIDFILE
159 [ "$VERBOSE" != no ] && [ "$RETVAL" = 1 ] && log_progress_msg "---> was not running.."
160 [ "$VERBOSE" != no -a "$RETVAL" = 0 ] && log_progress_msg "---> stopped"
161 return "$RETVAL"
162}
163
164#
165# Function that sends a SIGHUP to the daemon/service
166#
167do_reload() {
168 #
169 # If the daemon can reload its configuration without
170 # restarting (for example, when it is sent a SIGHUP),
171 # then implement that here.
172 #
173 start-stop-daemon --stop --quiet --signal 1 --pidfile $PIDFILE --chuid $NODEUSER --name $NAME
174 return 0
175}
176
177#
178# Function that returns the daemon
179#
180do_status() {
181 #
182 # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
183 # 0 program is running or service is OK
184 # 1 program is dead and /var/run pid file exists
185 # (2 program is dead and /var/lock lock file exists) (not used here)
186 # 3 program is not running
187 # 4 program or service status is unknown
188 RUNNING=$(running)
189
190 # $PIDFILE corresponds to a live $NAME process
191 ispidactive=$(pidof $NAME | grep `cat $PIDFILE 2>&1` >/dev/null 2>&1)
192 ISPIDACTIVE=$?
193
194 if [ -n "$RUNNING" ]; then
195 if [ $ISPIDACTIVE ]; then
196 log_success_msg "$INIT_SCRIPT_NAME_NOEXT launched by $NODEUSER is running"
197 exit 0
198 fi
199 else
200 if [ -f $PIDFILE ]; then
201 log_success_msg "$INIT_SCRIPT_NAME_NOEXT launched by $NODEUSER is not running, phantom pidfile $PIDFILE"
202 exit 1
203 else
204 log_success_msg "Not running. No instance of $INIT_SCRIPT_NAME_NOEXT launched by $NODEUSER found"
205 exit 3
206 fi
207 fi
208
209}
210
211running() {
212 RUNSTAT=$(start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $NODEUSER --background --exec $DAEMON --test > /dev/null)
213 if [ "$?" = 1 ]; then
214 echo y
215 fi
216
217}
218
219
220case "$1" in
221 start)
222 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$INIT_SCRIPT_NAME_NOEXT"
223 do_start
224 case "$?" in
225 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
226 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
227 esac
228 ;;
229 stop)
230 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$INIT_SCRIPT_NAME_NOEXT"
231 do_stop
232 case "$?" in
233 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
234 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
235 esac
236 ;;
237 #reload|force-reload)
238 #
239 # If do_reload() is not implemented then leave this commented out
240 # and leave 'force-reload' as an alias for 'restart'.
241 #
242 #log_daemon_msg "Reloading $DESC" "$NAME"
243 #do_reload
244 #log_end_msg $?
245 #;;
246 restart|force-reload)
247 #
248 # If the "reload" option is implemented then remove the
249 # 'force-reload' alias
250 #
251 [ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$INIT_SCRIPT_NAME_NOEXT"
252 do_stop
253 case "$?" in
254 0|1)
255 do_start
256 case "$?" in
257 0) log_end_msg 0 ;;
258 1) log_end_msg 1 ;; # Old process is still running
259 *) log_end_msg 1 ;; # Failed to start
260 esac
261 ;;
262 *)
263 # Failed to stop
264 log_end_msg 1
265 ;;
266 esac
267 ;;
268 status)
269 do_status
270 ;;
271 *)
272 #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
273 echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
274 exit 3
275 ;;
276esac
277
278exit 0
279