-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforever_nodejs_op.sh
executable file
·177 lines (156 loc) · 3.65 KB
/
forever_nodejs_op.sh
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
#!/bin/bash
#
# Service script for a Node.js application running under Forever.
#
# global vars used:
# NAME - name of the application to be started
# SOURCE_DIR - source dir where the applications resides
# PROJECT_DIR - application's home diretory
# SOURCE_FILE - application's binary file
# USAGE - usage message
# user - process owner's username
# outlog - Forever's output logs
# errlog - Forever's output error log
# logdir - logs directory
# node - Node.js's binary file
# forever - Forever's binary file
# sed - sed's binary file
# awk - awk's binary file
# forever_dir - base path for all forever related files (pid files, etc.)
# pidfile - application's pid file
# foreverid - Forever id
#
# description: Script for running Node.js apps with Forever
# created by: rotarur
# date: 25/03/2015
# Source function library.
. /etc/init.d/functions
start() {
echo "Starting $NAME node instance... "
if [ "$foreverid" == "" ]; then
# Create the log and pid files
# make sure that the target user has access to them
touch $outlog
#chown $user $logfile # if necessary
touch $pidfile
#chown $user $pidfile # if necessary
# Launch the application
$forever start --uid $user -p $forever_dir --pidFile $pidfile \
-o $outlog -e $errlog \
-a -d $PROJECT_DIR/$SOURCE_FILE
RETVAL=$?
else
echo "Instance already running!"
RETVAL=0
fi
}
restart() {
echo -n "Restarting $NAME node instance : "
if [ "$foreverid" != "" ]; then
$forever restart -p $forever_dir $foreverid
else
echo "Instance is not running";
fi
RETVAL=$?
}
stop() {
echo -n "Shutting down $NAME node instance : "
if [ "$foreverid" != "" ]; then
$forever stop -p $forever_dir $foreverid
else
echo "Instance is not running";
fi
RETVAL=$?
}
graceful() {
echo -n "Shutting down gracefuly..."
if [ "$foreverid" != "" ]; then
$forever stop --killSignal=SIGTERM -p $forever_dir $foreverid
else
echo "Instance is not running";
fi
RETVAL=$?
}
NAME=$2
if [ "$NAME" == "" ]; then
echo $USAGE
exit 1
fi
SOURCE_DIR="$HOME/workspace"
if [ ! -d "$SOURCE_DIR" ]; then
echo "> $SOURCE_DIR < does not exist or cannot be accessed."
echo "Verify if the folder exists and if it has the right permissions."
exit 1
fi
PROJECT_DIR="$SOURCE_DIR/$NAME"
if [ ! -d "$PROJECT_DIR" ]; then
echo "Please verify the >$PROJECT_DIR<."
exit 1
fi
logdir="$PROJECT_DIR/logs"
if [ ! -d $logdir ]; then
echo "$pid dir does not exist. Creating..."
mkdir $logdir
fi
SOURCE_FILE="app.js"
USAGE="Usage: forever_nodejs_op.sh {start|restart|stop|graceful|status} <app name>"
user="rr"
outlog="$PROJECT_DIR/logs/$NAME.log"
errlog="$PROJECT_DIR/logs/$NAME.err"
node=`which node`
if [ "$node" == "" ]; then
echo "Node.js not found."
exit 1
fi
forever=`which forever`
if [ "$forever" == "" ]; then
echo "Forever not found."
exit 1
fi
sed=`which sed`
awk=`which awk`
forever_dir="$PROJECT_DIR/forever"
if [ ! -d $forever_dir ]; then
echo "$forever_dir does not exit. Creating..."
mkdir $forever_dir
fi
pidfile="$PROJECT_DIR/logs/$NAME.pid"
if [ -f $pidfile ]; then
read pid < $pidfile
else
pid=""
fi
if [ "$pid" != "" ]; then
# Gnarly sed usage to obtain the foreverid.
#sed1="/$pid\]/p"
sed1="/$pid/p"
sed2="s/.*\[\([0-9]\+\)\].*\s$pid\.*/\1/g"
foreverid=`$forever list -p $forever_dir | $sed -n $sed1 | $awk '{print $7}'`
else
foreverid=""
fi
case "$1" in
start)
start
;;
restart)
restart
;;
stop)
stop
;;
status)
# warning: only tested for one application running.
# test with two at minimum application
$forever list -p ${foreverid}
RETVAL=$?
;;
graceful)
graceful
;;
*)
echo $USAGE
exit 1
;;
esac
exit $RETVAL