obazoud ha revisionato questo gist 14 years ago. Vai alla revisione
1 file changed, 6 insertions, 6 deletions
node_debian_init.sh
| @@ -57,6 +57,8 @@ | |||
| 57 | 57 | # PATH should only include /usr/* if it runs after the mountnfs.sh script | |
| 58 | 58 | PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin # modify if you need | |
| 59 | 59 | ||
| 60 | + | LOCAL_VAR_LOGS_DIR=/usr/local/var/log | |
| 61 | + | LOCAL_VAR_LOGS=$LOCAL_VAR_LOGS_DIR/nodejs.log | |
| 60 | 62 | DAEMON_ARGS="/path/to/app.js" # path to your node.js server/app | |
| 61 | 63 | # NB: don't use ~/ in path | |
| 62 | 64 | ||
| @@ -74,15 +76,14 @@ LOCAL_VAR_RUN=/usr/local/var/run # in case the init script is run by | |||
| 74 | 76 | ||
| 75 | 77 | NAME=node # name of the node.js executable | |
| 76 | 78 | DAEMON=/usr/local/bin/$NAME # this SHOULD POINT TO where your node executable is | |
| 79 | + | NODECMD="exec $DAEMON $DAEMON_ARGS >>$LOCAL_VAR_LOGS 2>&1" # node command | |
| 77 | 80 | # | |
| 78 | 81 | # # | |
| 79 | 82 | # END </MODIFY TO REFLECT YOUR SETTINGS> # | |
| 80 | 83 | # (Nothing else to modify from this point on...) # | |
| 81 | 84 | # ------------------------------------------------------------------------------ | |
| 82 | 85 | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | + | [ ! -d "$LOCAL_VAR_LOGS_DIR" ] || { mkdir "$LOCAL_VAR_LOGS_DIR" -p } | |
| 86 | 87 | ||
| 87 | 88 | # Do NOT "set -e" | |
| 88 | 89 | ||
| @@ -124,8 +125,7 @@ do_start() | |||
| 124 | 125 | # 2 if daemon could not be started | |
| 125 | 126 | start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $NODEUSER --background --exec $DAEMON --test > /dev/null \ | |
| 126 | 127 | || { [ "$VERBOSE" != no ] && log_daemon_msg " ---> Daemon already running $DESC" "$INIT_SCRIPT_NAME_NOEXT"; return 1; } | |
| 127 | - | start-stop-daemon --start --quiet --chuid $NODEUSER --make-pidfile --pidfile $PIDFILE --background --exec $DAEMON -- \ | |
| 128 | - | $DAEMON_ARGS \ | |
| 128 | + | start-stop-daemon --start --quiet --chuid $NODEUSER --make-pidfile --pidfile $PIDFILE --background --exec $DAEMON --startas /bin/sh -- -c "$NODECMD" \ | |
| 129 | 129 | || { [ "$VERBOSE" != no ] && log_daemon_msg " ---> could not be start $DESC" "$INIT_SCRIPT_NAME_NOEXT"; return 2; } | |
| 130 | 130 | # Add code here, if necessary, that waits for the process to be ready | |
| 131 | 131 | # to handle requests from services started subsequently which depend | |
| @@ -143,7 +143,7 @@ do_stop() | |||
| 143 | 143 | # 1 if daemon was already stopped | |
| 144 | 144 | # 2 if daemon could not be stopped | |
| 145 | 145 | # other if a failure occurred | |
| 146 | - | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --chuid $NODEUSER --name $DAEMON | |
| 146 | + | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --chuid $NODEUSER --exec $DAEMON | |
| 147 | 147 | RETVAL="$?" | |
| 148 | 148 | #[ "$VERBOSE" != no ] && [ "$RETVAL" = 1 ] && log_daemon_msg " ---> SIGKILL failed => hardkill $DESC" "$INIT_SCRIPT_NAME_NOEXT" | |
| 149 | 149 | [ "$RETVAL" = 2 ] && return 2 | |
Peter Host ha revisionato questo gist 15 years ago. Vai alla revisione
1 file changed, 4 insertions, 3 deletions
node_debian_init.sh
| @@ -89,6 +89,7 @@ DAEMON=/usr/local/bin/$NAME # this SHOULD POINT TO where your no | |||
| 89 | 89 | [ $UID -eq "0" ] && LOCAL_VAR_RUN=/var/run # in case this script is run by root, override user setting | |
| 90 | 90 | THIS_ARG=$0 | |
| 91 | 91 | INIT_SCRIPT_NAME=`basename $THIS_ARG` | |
| 92 | + | [ -h $THIS_ARG ] && INIT_SCRIPT_NAME=`basename $(readlink $THIS_ARG)` # in case of symlink | |
| 92 | 93 | INIT_SCRIPT_NAME_NOEXT=${INIT_SCRIPT_NAME%.*} | |
| 93 | 94 | PIDFILE="$LOCAL_VAR_RUN/$INIT_SCRIPT_NAME_NOEXT.pid" | |
| 94 | 95 | SCRIPTNAME=/etc/init.d/$INIT_SCRIPT_NAME | |
| @@ -193,15 +194,15 @@ do_status() { | |||
| 193 | 194 | ||
| 194 | 195 | if [ -n "$RUNNING" ]; then | |
| 195 | 196 | if [ $ISPIDACTIVE ]; then | |
| 196 | - | log_success_msg "$INIT_SCRIPT_NAME_NOEXT ($NODEUSER) is running" | |
| 197 | + | log_success_msg "$INIT_SCRIPT_NAME_NOEXT (launched by $USER) (--chuid $NODEUSER) is running" | |
| 197 | 198 | exit 0 | |
| 198 | 199 | fi | |
| 199 | 200 | else | |
| 200 | 201 | if [ -f $PIDFILE ]; then | |
| 201 | - | log_success_msg "$INIT_SCRIPT_NAME_NOEXT ($NODEUSER) is not running, phantom pidfile $PIDFILE" | |
| 202 | + | log_success_msg "$INIT_SCRIPT_NAME_NOEXT (launched by $USER) (--chuid $NODEUSER) is not running, phantom pidfile $PIDFILE" | |
| 202 | 203 | exit 1 | |
| 203 | 204 | else | |
| 204 | - | log_success_msg "$INIT_SCRIPT_NAME_NOEXT ($NODEUSER) is not running" | |
| 205 | + | log_success_msg "no instance launched by $USER, of $INIT_SCRIPT_NAME_NOEXT (--chuid $NODEUSER) found" | |
| 205 | 206 | exit 3 | |
| 206 | 207 | fi | |
| 207 | 208 | fi | |
Peter Host ha revisionato questo gist 15 years ago. Vai alla revisione
1 file changed, 30 insertions, 20 deletions
node_debian_init.sh
| @@ -2,32 +2,38 @@ | |||
| 2 | 2 | # ------------------------------------------------------------------------------ | |
| 3 | 3 | # SOME INFOS : fairly standard (debian) init script. | |
| 4 | 4 | # Note that node doesn't create a PID file (hence --make-pidfile) | |
| 5 | - | # has to be run node in the background (hence --background) | |
| 5 | + | # has to be run in the background (hence --background) | |
| 6 | 6 | # and NOT as root (hence --chuid) | |
| 7 | 7 | # | |
| 8 | 8 | # MORE INFOS : INIT SCRIPT http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit | |
| 9 | 9 | # INIT-INFO RULES http://wiki.debian.org/LSBInitScripts | |
| 10 | 10 | # INSTALL/REMOVE http://www.debian-administration.org/articles/28 | |
| 11 | 11 | # ------------------------------------------------------------------------------ | |
| 12 | - | ## BEGIN <MODIFY TO REFLECT YOUR SETTINGS> ## | |
| 12 | + | # # | |
| 13 | + | # BEGIN <MODIFY TO REFLECT YOUR SETTINGS> # | |
| 14 | + | # # | |
| 13 | 15 | # | |
| 14 | 16 | # 1) Don't forget to also modify the COMMENTED fields in the "### BEGIN INIT INFO" | |
| 15 | - | # below (don't uncomment them) | |
| 16 | - | # to reflect your setting before issuing update-rc.d | |
| 17 | + | # below (don't uncomment them) if you wish to install system-wide with update-rc.d | |
| 17 | 18 | # eg: provides, Short-Description, Description | |
| 18 | 19 | # | |
| 19 | 20 | # 2) in case you have different node.js servers running, each init.d script should | |
| 20 | - | # (obviously) have a unique name so that PIDS do not conflict | |
| 21 | + | # (obviously) have a UNIQUE BASE name so that PIDS do not conflict | |
| 21 | 22 | # --> name them accordingly | |
| 22 | 23 | # eg: node_static_server, node_express1, node_load_balancer.sh... | |
| 23 | 24 | # | |
| 24 | 25 | # 3) copy the renamed/modified script(s) to /etc/init.d | |
| 25 | 26 | # chmod 755, | |
| 27 | + | # | |
| 28 | + | # 4) if you wish the Daemon to be lauched at boot / stopped at shutdown : | |
| 26 | 29 | # INSTALL : update-rc.d scriptname defaults | |
| 27 | - | # UNINSTALL : update-rc.d -f scriptname remove | |
| 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) | |
| 28 | 34 | # ______________________________________________________________________________ | |
| 29 | 35 | ### BEGIN INIT INFO | |
| 30 | - | # Provides: nodeserver | |
| 36 | + | # Provides: node_debian_init | |
| 31 | 37 | # Required-Start: $remote_fs $named $syslog | |
| 32 | 38 | # Required-Stop: $remote_fs $named $syslog | |
| 33 | 39 | # Default-Start: 2 3 4 5 | |
| @@ -36,13 +42,13 @@ | |||
| 36 | 42 | # Description: ex : proxy server is a node.js http server listening on | |
| 37 | 43 | # port 8080 (relayed from 80 by iptables). It balances | |
| 38 | 44 | # http requests between the main nodejs server | |
| 39 | - | # (nodejs.oghme.com:8000), the static file-server | |
| 40 | - | # (static.oghme.com) and the legacy apache server | |
| 41 | - | # (apache.oghme.com) and possibly other servers | |
| 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 | |
| 42 | 48 | # place this file in /etc/init.d. | |
| 43 | 49 | ### END INIT INFO | |
| 44 | 50 | ||
| 45 | - | # Author: Peter Host <peterhost+oghme@virtualregions.com> | |
| 51 | + | # Author: My Name <myname@domain.tld> | |
| 46 | 52 | # | |
| 47 | 53 | # Please remove the "Author" lines above and replace them | |
| 48 | 54 | # with your own name if you copy and modify this script. | |
| @@ -51,10 +57,12 @@ | |||
| 51 | 57 | # PATH should only include /usr/* if it runs after the mountnfs.sh script | |
| 52 | 58 | PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin # modify if you need | |
| 53 | 59 | ||
| 60 | + | DAEMON_ARGS="/path/to/app.js" # path to your node.js server/app | |
| 61 | + | # NB: don't use ~/ in path | |
| 54 | 62 | ||
| 55 | 63 | DESC="node.js http server" # whatever fancy description you like | |
| 56 | 64 | ||
| 57 | - | NODEUSER=tomneale:tomneale # USER who OWNS the daemon process (no matter whoever runs the init script) | |
| 65 | + | NODEUSER=myuser:mygroup # USER who OWNS the daemon process (no matter whoever runs the init script) | |
| 58 | 66 | # user:group (if no group is specified, the primary GID for that user is used) | |
| 59 | 67 | ||
| 60 | 68 | LOCAL_VAR_RUN=/usr/local/var/run # in case the init script is run by non-root user, you need to | |
| @@ -66,14 +74,16 @@ LOCAL_VAR_RUN=/usr/local/var/run # in case the init script is run by | |||
| 66 | 74 | ||
| 67 | 75 | NAME=node # name of the node.js executable | |
| 68 | 76 | DAEMON=/usr/local/bin/$NAME # this SHOULD POINT TO where your node executable is | |
| 69 | - | DAEMON_ARGS="/home/tomneale/STATIC/app.js" # your node.js server/app goes here | |
| 70 | - | # NB: don't use ~/ in path | |
| 71 | - | ||
| 72 | - | ## END </MODIFY TO REFLECT YOUR SETTINGS> ## | |
| 73 | - | ## (Nothing else to modify from this point on...) ## | |
| 77 | + | # | |
| 78 | + | # # | |
| 79 | + | # END </MODIFY TO REFLECT YOUR SETTINGS> # | |
| 80 | + | # (Nothing else to modify from this point on...) # | |
| 74 | 81 | # ------------------------------------------------------------------------------ | |
| 75 | 82 | ||
| 76 | 83 | ||
| 84 | + | ||
| 85 | + | ||
| 86 | + | ||
| 77 | 87 | # Do NOT "set -e" | |
| 78 | 88 | ||
| 79 | 89 | [ $UID -eq "0" ] && LOCAL_VAR_RUN=/var/run # in case this script is run by root, override user setting | |
| @@ -99,8 +109,8 @@ SCRIPTNAME=/etc/init.d/$INIT_SCRIPT_NAME | |||
| 99 | 109 | # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. | |
| 100 | 110 | . /lib/lsb/init-functions | |
| 101 | 111 | ||
| 102 | - | # REMOVE THAT !!! | |
| 103 | - | VERBOSE=yes | |
| 112 | + | # uncomment to override system setting | |
| 113 | + | # VERBOSE=yes | |
| 104 | 114 | ||
| 105 | 115 | # | |
| 106 | 116 | # Function that starts the daemon/service | |
| @@ -134,7 +144,7 @@ do_stop() | |||
| 134 | 144 | # other if a failure occurred | |
| 135 | 145 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --chuid $NODEUSER --name $DAEMON | |
| 136 | 146 | RETVAL="$?" | |
| 137 | - | [ "$VERBOSE" != no ] && [ "$RETVAL" = 1 ] && log_daemon_msg " ---> SIGKILL failed => hardkill $DESC" "$INIT_SCRIPT_NAME_NOEXT" | |
| 147 | + | #[ "$VERBOSE" != no ] && [ "$RETVAL" = 1 ] && log_daemon_msg " ---> SIGKILL failed => hardkill $DESC" "$INIT_SCRIPT_NAME_NOEXT" | |
| 138 | 148 | [ "$RETVAL" = 2 ] && return 2 | |
| 139 | 149 | # Wait for children to finish too if this is a daemon that forks | |
| 140 | 150 | # and if the daemon is only ever run from this initscript. | |
Peter Host ha revisionato questo gist 15 years ago. Vai alla revisione
1 file changed, 67 insertions, 31 deletions
node_debian_init.sh
| @@ -2,38 +2,32 @@ | |||
| 2 | 2 | # ------------------------------------------------------------------------------ | |
| 3 | 3 | # SOME INFOS : fairly standard (debian) init script. | |
| 4 | 4 | # Note that node doesn't create a PID file (hence --make-pidfile) | |
| 5 | - | # has to be run in the background (hence --background) | |
| 5 | + | # has to be run node in the background (hence --background) | |
| 6 | 6 | # and NOT as root (hence --chuid) | |
| 7 | 7 | # | |
| 8 | 8 | # MORE INFOS : INIT SCRIPT http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit | |
| 9 | 9 | # INIT-INFO RULES http://wiki.debian.org/LSBInitScripts | |
| 10 | 10 | # INSTALL/REMOVE http://www.debian-administration.org/articles/28 | |
| 11 | 11 | # ------------------------------------------------------------------------------ | |
| 12 | - | # # | |
| 13 | - | # BEGIN <MODIFY TO REFLECT YOUR SETTINGS> # | |
| 14 | - | # # | |
| 12 | + | ## BEGIN <MODIFY TO REFLECT YOUR SETTINGS> ## | |
| 15 | 13 | # | |
| 16 | 14 | # 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 | |
| 15 | + | # below (don't uncomment them) | |
| 16 | + | # to reflect your setting before issuing update-rc.d | |
| 18 | 17 | # eg: provides, Short-Description, Description | |
| 19 | 18 | # | |
| 20 | 19 | # 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 | |
| 20 | + | # (obviously) have a unique name so that PIDS do not conflict | |
| 22 | 21 | # --> name them accordingly | |
| 23 | 22 | # eg: node_static_server, node_express1, node_load_balancer.sh... | |
| 24 | 23 | # | |
| 25 | 24 | # 3) copy the renamed/modified script(s) to /etc/init.d | |
| 26 | 25 | # chmod 755, | |
| 27 | - | # | |
| 28 | - | # 4) if you wish the Daemon to be lauched at boot / stopped at shutdown : | |
| 29 | 26 | # 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) | |
| 27 | + | # UNINSTALL : update-rc.d -f scriptname remove | |
| 34 | 28 | # ______________________________________________________________________________ | |
| 35 | 29 | ### BEGIN INIT INFO | |
| 36 | - | # Provides: node_debian_init | |
| 30 | + | # Provides: nodeserver | |
| 37 | 31 | # Required-Start: $remote_fs $named $syslog | |
| 38 | 32 | # Required-Stop: $remote_fs $named $syslog | |
| 39 | 33 | # Default-Start: 2 3 4 5 | |
| @@ -42,13 +36,13 @@ | |||
| 42 | 36 | # Description: ex : proxy server is a node.js http server listening on | |
| 43 | 37 | # port 8080 (relayed from 80 by iptables). It balances | |
| 44 | 38 | # 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 | |
| 39 | + | # (nodejs.oghme.com:8000), the static file-server | |
| 40 | + | # (static.oghme.com) and the legacy apache server | |
| 41 | + | # (apache.oghme.com) and possibly other servers | |
| 48 | 42 | # place this file in /etc/init.d. | |
| 49 | 43 | ### END INIT INFO | |
| 50 | 44 | ||
| 51 | - | # Author: Peter Host <myname@domain.tld> | |
| 45 | + | # Author: Peter Host <peterhost+oghme@virtualregions.com> | |
| 52 | 46 | # | |
| 53 | 47 | # Please remove the "Author" lines above and replace them | |
| 54 | 48 | # with your own name if you copy and modify this script. | |
| @@ -57,12 +51,10 @@ | |||
| 57 | 51 | # PATH should only include /usr/* if it runs after the mountnfs.sh script | |
| 58 | 52 | PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin # modify if you need | |
| 59 | 53 | ||
| 60 | - | DAEMON_ARGS="/path/to/app.js" # path to your node.js server/app | |
| 61 | - | # NB: don't use ~/ in path | |
| 62 | 54 | ||
| 63 | 55 | DESC="node.js http server" # whatever fancy description you like | |
| 64 | 56 | ||
| 65 | - | NODEUSER=myuser:mygroup # USER who OWNS the daemon process (no matter whoever runs the init script) | |
| 57 | + | NODEUSER=tomneale:tomneale # USER who OWNS the daemon process (no matter whoever runs the init script) | |
| 66 | 58 | # user:group (if no group is specified, the primary GID for that user is used) | |
| 67 | 59 | ||
| 68 | 60 | LOCAL_VAR_RUN=/usr/local/var/run # in case the init script is run by non-root user, you need to | |
| @@ -74,14 +66,12 @@ LOCAL_VAR_RUN=/usr/local/var/run # in case the init script is run by | |||
| 74 | 66 | ||
| 75 | 67 | NAME=node # name of the node.js executable | |
| 76 | 68 | DAEMON=/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 | - | ||
| 69 | + | DAEMON_ARGS="/home/tomneale/STATIC/app.js" # your node.js server/app goes here | |
| 70 | + | # NB: don't use ~/ in path | |
| 84 | 71 | ||
| 72 | + | ## END </MODIFY TO REFLECT YOUR SETTINGS> ## | |
| 73 | + | ## (Nothing else to modify from this point on...) ## | |
| 74 | + | # ------------------------------------------------------------------------------ | |
| 85 | 75 | ||
| 86 | 76 | ||
| 87 | 77 | # Do NOT "set -e" | |
| @@ -109,8 +99,8 @@ SCRIPTNAME=/etc/init.d/$INIT_SCRIPT_NAME | |||
| 109 | 99 | # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. | |
| 110 | 100 | . /lib/lsb/init-functions | |
| 111 | 101 | ||
| 112 | - | # uncomment to override system setting | |
| 113 | - | # VERBOSE=yes | |
| 102 | + | # REMOVE THAT !!! | |
| 103 | + | VERBOSE=yes | |
| 114 | 104 | ||
| 115 | 105 | # | |
| 116 | 106 | # Function that starts the daemon/service | |
| @@ -144,7 +134,7 @@ do_stop() | |||
| 144 | 134 | # other if a failure occurred | |
| 145 | 135 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --chuid $NODEUSER --name $DAEMON | |
| 146 | 136 | RETVAL="$?" | |
| 147 | - | #[ "$VERBOSE" != no ] && [ "$RETVAL" = 1 ] && log_daemon_msg " ---> SIGKILL failed => hardkill $DESC" "$INIT_SCRIPT_NAME_NOEXT" | |
| 137 | + | [ "$VERBOSE" != no ] && [ "$RETVAL" = 1 ] && log_daemon_msg " ---> SIGKILL failed => hardkill $DESC" "$INIT_SCRIPT_NAME_NOEXT" | |
| 148 | 138 | [ "$RETVAL" = 2 ] && return 2 | |
| 149 | 139 | # Wait for children to finish too if this is a daemon that forks | |
| 150 | 140 | # and if the daemon is only ever run from this initscript. | |
| @@ -174,6 +164,49 @@ do_reload() { | |||
| 174 | 164 | return 0 | |
| 175 | 165 | } | |
| 176 | 166 | ||
| 167 | + | # | |
| 168 | + | # Function that returns the daemon | |
| 169 | + | # | |
| 170 | + | do_status() { | |
| 171 | + | # | |
| 172 | + | # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html | |
| 173 | + | # 0 program is running or service is OK | |
| 174 | + | # 1 program is dead and /var/run pid file exists | |
| 175 | + | # (2 program is dead and /var/lock lock file exists) (not used here) | |
| 176 | + | # 3 program is not running | |
| 177 | + | # 4 program or service status is unknown | |
| 178 | + | RUNNING=$(running) | |
| 179 | + | ||
| 180 | + | # $PIDFILE corresponds to a live $NAME process | |
| 181 | + | ispidactive=$(pidof $NAME | grep `cat $PIDFILE 2>&1` >/dev/null 2>&1) | |
| 182 | + | ISPIDACTIVE=$? | |
| 183 | + | ||
| 184 | + | if [ -n "$RUNNING" ]; then | |
| 185 | + | if [ $ISPIDACTIVE ]; then | |
| 186 | + | log_success_msg "$INIT_SCRIPT_NAME_NOEXT ($NODEUSER) is running" | |
| 187 | + | exit 0 | |
| 188 | + | fi | |
| 189 | + | else | |
| 190 | + | if [ -f $PIDFILE ]; then | |
| 191 | + | log_success_msg "$INIT_SCRIPT_NAME_NOEXT ($NODEUSER) is not running, phantom pidfile $PIDFILE" | |
| 192 | + | exit 1 | |
| 193 | + | else | |
| 194 | + | log_success_msg "$INIT_SCRIPT_NAME_NOEXT ($NODEUSER) is not running" | |
| 195 | + | exit 3 | |
| 196 | + | fi | |
| 197 | + | fi | |
| 198 | + | ||
| 199 | + | } | |
| 200 | + | ||
| 201 | + | running() { | |
| 202 | + | RUNSTAT=$(start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $NODEUSER --background --exec $DAEMON --test > /dev/null) | |
| 203 | + | if [ "$?" = 1 ]; then | |
| 204 | + | echo y | |
| 205 | + | fi | |
| 206 | + | ||
| 207 | + | } | |
| 208 | + | ||
| 209 | + | ||
| 177 | 210 | case "$1" in | |
| 178 | 211 | start) | |
| 179 | 212 | [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$INIT_SCRIPT_NAME_NOEXT" | |
| @@ -222,6 +255,9 @@ case "$1" in | |||
| 222 | 255 | ;; | |
| 223 | 256 | esac | |
| 224 | 257 | ;; | |
| 258 | + | status) | |
| 259 | + | do_status | |
| 260 | + | ;; | |
| 225 | 261 | *) | |
| 226 | 262 | #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 | |
| 227 | 263 | echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 | |
| @@ -229,4 +265,4 @@ case "$1" in | |||
| 229 | 265 | ;; | |
| 230 | 266 | esac | |
| 231 | 267 | ||
| 232 | - | : | |
| 268 | + | exit 0 | |
Peter Host ha revisionato questo gist 15 years ago. Vai alla revisione
1 file changed, 232 insertions
node_debian_init.sh(file creato)
| @@ -0,0 +1,232 @@ | |||
| 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 | |
| 58 | + | PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin # modify if you need | |
| 59 | + | ||
| 60 | + | DAEMON_ARGS="/path/to/app.js" # path to your node.js server/app | |
| 61 | + | # NB: don't use ~/ in path | |
| 62 | + | ||
| 63 | + | DESC="node.js http server" # whatever fancy description you like | |
| 64 | + | ||
| 65 | + | NODEUSER=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 | + | ||
| 68 | + | LOCAL_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 | + | ||
| 75 | + | NAME=node # name of the node.js executable | |
| 76 | + | DAEMON=/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 | |
| 90 | + | THIS_ARG=$0 | |
| 91 | + | INIT_SCRIPT_NAME=`basename $THIS_ARG` | |
| 92 | + | INIT_SCRIPT_NAME_NOEXT=${INIT_SCRIPT_NAME%.*} | |
| 93 | + | PIDFILE="$LOCAL_VAR_RUN/$INIT_SCRIPT_NAME_NOEXT.pid" | |
| 94 | + | SCRIPTNAME=/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 | + | # | |
| 118 | + | do_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 | + | # | |
| 138 | + | do_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 | + | # | |
| 167 | + | do_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 | + | case "$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 | + | ;; | |
| 230 | + | esac | |
| 231 | + | ||
| 232 | + | : | |