-
Notifications
You must be signed in to change notification settings - Fork 169
/
node-app
executable file
·188 lines (163 loc) · 3.44 KB
/
node-app
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
181
182
183
184
185
186
187
188
#!/bin/sh
APP_NAME="app"
USER="root"
GROUP="$USER"
NODE_ENV="production"
PORT="3000"
APP_DIR="/var/www/example.com"
NODE_APP="app.js"
KWARGS=""
CONFIG_DIR="$APP_DIR"
PID_DIR="$APP_DIR/pid"
PID_FILE="$PID_DIR/$APP_NAME.pid"
LOG_DIR="$APP_DIR/log"
LOG_FILE="$LOG_DIR/$APP_NAME.log"
NODE_EXEC=$(which node)
###############
# REDHAT chkconfig header
# chkconfig: - 58 74
# description: node-app is the script for starting a node app on boot.
### BEGIN INIT INFO
# Provides: node
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop node
# Description: Node process for app
### END INIT INFO
###############
USAGE="Usage: $0 {start|stop|restart|status} [--force]"
FORCE_OP=false
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
pid_file_exists() {
[ -f "$PID_FILE" ]
}
get_pid() {
echo "$(cat "$PID_FILE")"
}
is_running() {
PID="$(get_pid)"
[ -d /proc/$PID ]
}
start_it() {
mkdir -p "$PID_DIR"
chown $USER:$GROUP "$PID_DIR"
mkdir -p "$LOG_DIR"
chown $USER:$GROUP "$LOG_DIR"
echo "Starting $APP_NAME ..."
echo "cd $APP_DIR
if [ $? -ne 0 ]; then
exit
fi
set -a
PORT=$PORT
NODE_ENV=$NODE_ENV
NODE_CONFIG_DIR=$CONFIG_DIR
$NODE_EXEC \"$APP_DIR\"/$NODE_APP $KWARGS &>> \"$LOG_FILE\" &
echo \$! > $PID_FILE" | sudo -i -u $USER
echo "$APP_NAME started with pid $(get_pid)"
}
stop_process() {
PID=$(get_pid)
echo "Killing process $PID"
kill $PID
wait $PID 2>/dev/null
}
remove_pid_file() {
echo "Removing pid file"
rm -f "$PID_FILE"
}
start_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "$APP_NAME already running with pid $PID"
exit 1
else
echo "$APP_NAME stopped, but pid file exists"
if [ $FORCE_OP = true ]
then
echo "Forcing start anyways"
remove_pid_file
start_it
fi
fi
else
start_it
fi
}
stop_app() {
if pid_file_exists
then
if is_running
then
echo "Stopping $APP_NAME ..."
stop_process
remove_pid_file
echo "$APP_NAME stopped"
else
echo "$APP_NAME already stopped, but pid file exists"
if [ $FORCE_OP = true ]
then
echo "Forcing stop anyways ..."
remove_pid_file
echo "$APP_NAME stopped"
else
exit 1
fi
fi
else
echo "$APP_NAME already stopped, pid file does not exist"
exit 1
fi
}
status_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "$APP_NAME running with pid $PID"
else
echo "$APP_NAME stopped, but pid file exists"
fi
else
echo "$APP_NAME stopped"
fi
}
case "$2" in
--force)
FORCE_OP=true
;;
"")
;;
*)
echo $USAGE
exit 1
;;
esac
case "$1" in
start)
start_app
;;
stop)
stop_app
;;
restart)
stop_app
start_app
;;
status)
status_app
;;
*)
echo $USAGE
exit 1
;;
esac