Última atividade 6 months ago

Revisão 45f25b89f1df05b012c5b43f712d97164fce5741

node_debian_init.sh Bruto
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: Peter Host <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
60DAEMON_ARGS="/path/to/app.js" # path to your node.js server/app
61 # NB: don't use ~/ in path
62
63DESC="node.js http server" # whatever fancy description you like
64
65NODEUSER=myuser:mygroup # USER who OWNS the daemon process (no matter whoever runs the init script)
66 # user:group (if no group is specified, the primary GID for that user is used)
67
68LOCAL_VAR_RUN=/usr/local/var/run # in case the init script is run by non-root user, you need to
69 # indicate a directory writeable by $NODEUSER to store the PID file
70 # NB : 1) /usr/local/var/run does not exist by DEFAULT. Either create it
71 # or choose one of your own liking.
72 # 2) node, npm,... are best NOT installed/run as ROOT.
73 # (see here: https://github.com/isaacs/npm/blob/master/README.md)
74
75NAME=node # name of the node.js executable
76DAEMON=/usr/local/bin/$NAME # this SHOULD POINT TO where your node executable is
77#
78# #
79# END </MODIFY TO REFLECT YOUR SETTINGS> #
80# (Nothing else to modify from this point on...) #
81# ------------------------------------------------------------------------------
82
83
84
85
86
87# Do NOT "set -e"
88
89[ $UID -eq "0" ] && LOCAL_VAR_RUN=/var/run # in case this script is run by root, override user setting
90THIS_ARG=$0
91INIT_SCRIPT_NAME=`basename $THIS_ARG`
92INIT_SCRIPT_NAME_NOEXT=${INIT_SCRIPT_NAME%.*}
93PIDFILE="$LOCAL_VAR_RUN/$INIT_SCRIPT_NAME_NOEXT.pid"
94SCRIPTNAME=/etc/init.d/$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# Read configuration variable file if it is present
103[ -r /etc/default/$INIT_SCRIPT_NAME ] && . /etc/default/$INIT_SCRIPT_NAME
104
105# Load the VERBOSE setting and other rcS variables
106. /lib/init/vars.sh
107
108# Define LSB log_* functions.
109# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
110. /lib/lsb/init-functions
111
112# uncomment to override system setting
113# VERBOSE=yes
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_daemon_msg " ---> Daemon already running $DESC" "$INIT_SCRIPT_NAME_NOEXT"; return 1; }
126 start-stop-daemon --start --quiet --chuid $NODEUSER --make-pidfile --pidfile $PIDFILE --background --exec $DAEMON -- \
127 $DAEMON_ARGS \
128 || { [ "$VERBOSE" != no ] && log_daemon_msg " ---> could not be start $DESC" "$INIT_SCRIPT_NAME_NOEXT"; return 2; }
129 # Add code here, if necessary, that waits for the process to be ready
130 # to handle requests from services started subsequently which depend
131 # on this one. As a last resort, sleep for some time.
132 [ "$VERBOSE" != no ] && log_daemon_msg " ---> started $DESC" "$INIT_SCRIPT_NAME_NOEXT"
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 --name $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_daemon_msg " ---> $DESC not running" "$INIT_SCRIPT_NAME_NOEXT"
160 [ "$VERBOSE" != no -a "$RETVAL" = 0 ] && log_daemon_msg " ---> $DESC stopped" "$INIT_SCRIPT_NAME_NOEXT"
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
177case "$1" in
178 start)
179 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$INIT_SCRIPT_NAME_NOEXT"
180 do_start
181 case "$?" in
182 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
183 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
184 esac
185 ;;
186 stop)
187 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$INIT_SCRIPT_NAME_NOEXT"
188 do_stop
189 case "$?" in
190 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
191 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
192 esac
193 ;;
194 #reload|force-reload)
195 #
196 # If do_reload() is not implemented then leave this commented out
197 # and leave 'force-reload' as an alias for 'restart'.
198 #
199 #log_daemon_msg "Reloading $DESC" "$NAME"
200 #do_reload
201 #log_end_msg $?
202 #;;
203 restart|force-reload)
204 #
205 # If the "reload" option is implemented then remove the
206 # 'force-reload' alias
207 #
208 log_daemon_msg "Restarting $DESC" "$INIT_SCRIPT_NAME_NOEXT"
209 do_stop
210 case "$?" in
211 0|1)
212 do_start
213 case "$?" in
214 0) log_end_msg 0 ;;
215 1) log_end_msg 1 ;; # Old process is still running
216 *) log_end_msg 1 ;; # Failed to start
217 esac
218 ;;
219 *)
220 # Failed to stop
221 log_end_msg 1
222 ;;
223 esac
224 ;;
225 *)
226 #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
227 echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
228 exit 3
229 ;;
230esac
231
232:
233