Ostatnio aktywny 6 months ago

Rewizja fd5180bffe6e99d0108af4b4b92d6f6ce821bf14

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