-
Notifications
You must be signed in to change notification settings - Fork 0
/
forever-initscript
executable file
·180 lines (169 loc) · 6.58 KB
/
forever-initscript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
#
################################################################
#
# Enable script:
#
# sudo update-rc.d <scriptname> defaults
# sudo update-rc.d <scriptname> enable
# sudo /etc/init.d/<scriptname> start
#
################################################################
#
# An example init script for running a Node.js process as a service
# using Forever as the process monitor. For more configuration options
# associated with Forever, see: https://github.com/nodejitsu/forever
#
# You will need to set the environment variables noted below to conform to
# your use case, and change the init info comment block.
#
# This was written for Debian distributions such as Ubuntu, but should still
# work on RedHat, Fedora, or other RPM-based distributions, since none
# of the built-in service functions are used. If you do adapt it to a RPM-based
# system, you'll need to replace the init info comment block with a chkconfig
# comment block.
#
### BEGIN INIT INFO
# Provides: "Whiteboard Serevr"
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: "Whiteboard Server"
# Description: "Starts the Whiteboard Server"
### END INIT INFO
#
# Based on:
# https://gist.github.com/3748766
# https://github.com/hectorcorrea/hectorcorrea.com/blob/master/etc/forever-initd-hectorcorrea.sh
# https://www.exratione.com/2011/07/running-a-nodejs-server-as-a-service-using-forever/
# Source function library. Note that this isn't used here, but remains to be
# uncommented by those who want to edit this script to add more functionality.
# Note that this is Ubuntu-specific. The scripts and script location are different on
# RPM-based distributions.
# . /lib/lsb/init-functions
# The example environment variables below assume that Node.js is
# installed into /home/node/local/node by building from source as outlined
# here:
# https://www.exratione.com/2011/07/running-a-nodejs-server-as-a-service-using-forever/
#
# It should be easy enough to adapt to the paths to be appropriate to a
# package installation, but note that the packages available for Ubuntu in
# the default repositories are far behind the times. Most users will be
# building from source to get a more recent Node.js version.
#
# An application name to display in echo text.
# NAME="My Application"
# The full path to the directory containing the node and forever binaries.
# NODE_BIN_DIR=/home/node/local/node/bin
# Set the NODE_PATH to the Node.js main node_modules directory.
# NODE_PATH=/home/node/local/node/lib/node_modules
# The directory containing the application start Javascript file.
# APPLICATION_DIRECTORY=/home/node/my-application
# The application start Javascript filename.
# APPLICATION_START=start-my-application.js
# Process ID file path.
# PIDFILE=/var/run/my-application.pid
# Log file path.
# LOGFILE=/var/log/my-application.log
#
NAME="whiteboard"
RUN_AS_USER=iudex
NODE_BIN_DIR=/usr/bin
NODE_PATH=/home/iudex/www/whiteboard/node_modules
APPLICATION_DIRECTORY=/home/iudex/www/whiteboard/
APPLICATION_START=server.js
PIDFILE=/home/iudex/www/whiteboard/w.pid
LOGFILE=/home/iudex/www/whiteboard/logs/w.log
OUTFILE=/home/iudex/www/whiteboard/logs/w.out
ERRFILE=/home/iudex/www/whiteboard/logs/w.err
#forever="FOREVER_ROOT=/etc/forever forever"
forever="forever"
# Add node to the path for situations in which the environment is passed.
PATH=$NODE_BIN_DIR:$PATH
# Export all environment variables that must be visible for the Node.js
# application process forked by Forever. It will not see any of the other
# variables defined in this script.
export NODE_PATH=$NODE_PATH
start() {
echo "Starting $NAME..."
# We're calling forever directly without using start-stop-daemon for the
# sake of simplicity when it comes to environment, and because this way
# the script will work whether it is executed directly or via the service
# utility.
#
# The minUptime and spinSleepTime settings stop Forever from thrashing if
# the application fails immediately on launch. This is generally necessary to
# avoid loading development servers to the point of failure every time
# someone makes an error in application initialization code, or bringing down
# production servers the same way if a database or other critical service
# suddenly becomes inaccessible.
#
# The pidfile contains the child process pid, not the forever process pid.
# We're only using it as a marker for whether or not the process is
# running.
# NOTE: We need to cd (as the ghost user account) so that ghost picks up the config file
if [ -f $PIDFILE ]; then
echo -e "\n\e[1;31mAbort!\e[0;31m Server \"$NAME\" already running!\e[0m\n pidfile: $PIDFILE";
else
su - $RUN_AS_USER -c "export NODE_ENV=production; cd $APPLICATION_DIRECTORY ; $forever --uid "$NAME" --pidFile $PIDFILE --sourceDir $APPLICATION_DIRECTORY \
-s -a -n 0 -l $LOGFILE -o $OUTFILE -e $ERRFILE --minUptime 5000 --spinSleepTime 2000 \
start $APPLICATION_START &"
echo -e "\n\e[32mStarted!\e[0m"
RETVAL=$?
fi
echo ""
}
stop() {
if [ -f $PIDFILE ]; then
echo "Shutting down $NAME..."
# Tell Forever to stop the process. Note that doing it this way means
# that each application that runs as a service must have a different
# start file name, regardless of which directory it is in.
su - $RUN_AS_USER -c "$forever --sourceDir $APPLICATION_DIRECTORY stop $NAME"
# Get rid of the pidfile, since Forever won't do that.
rm -f $PIDFILE
RETVAL=$?
else
echo "Server \"$NAME\" is not running."
RETVAL=0
fi
}
restart() {
echo -e "\e[1;33mRestarting $NAME\e[0m\n"
stop
start
}
status() {
echo "Status for $NAME:"
# This is taking the lazy way out on status, as it will return a list of
# all running Forever processes. You get to figure out what you want to
# know from that information.
#
# On Ubuntu, this isn't even necessary. To find out whether the service is
# running, use "service my-application status" which bypasses this script
# entirely provided you used the service utility to start the process.
su $RUN_AS_USER -c "$forever list -p $APPLICATION_DIRECTORY"
RETVAL=$?
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL