-
Notifications
You must be signed in to change notification settings - Fork 0
/
initd_script.sh
227 lines (206 loc) · 4.57 KB
/
initd_script.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash
### BEGIN INIT INFO
# Provides: Red Hat Service Daemon Template
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Red Hat Service Daemon Template
### END INIT INFO
# Generic Init.d Script for RHEL <= 6
# Set Below Variables
# By Levon Becker
# Last Updated 11/25/2014
# region Edit These Variables
servicename=myserviced
binary=/bin/bash
script=/opt/application/runscript.sh
logfile=/var/log/myserviced/myserviced.log
pidfile=/var/run/myserviced/myserviced.pid
user=myservice
# endregion Edit These Variables
program="$binary $script"
return_value=0
ensure_piddir() {
pid_dir=`dirname ${pidfile}`
if [ ! -d ${pid_dir} ]; then
mkdir -p ${pid_dir}
chown -R ${user} ${pid_dir}
chmod 755 ${pid_dir}
fi
}
search_pidproc() {
pid=
pid_running=false
pid=$(pgrep -P1 -fl "$program" | grep -v grep | grep -v bash | cut -f1 -d" ")
# If string not empty = true
if [ $pid ]; then
pid_running=true
fi
}
get_pidfile() {
pid=
read pid < "$pidfile"
}
check_pidproc() {
pid_found=false
if [ "$pid" ]; then
ps -p $pid > /dev/null
if [ $? -eq 0 ]; then
pid_found=true
echo " Service $servicename $pid [ RUNNING ]"
else
echo " Service $servicename $pid [ STOPPED ]"
fi
else
echo " Service $servicename $pid [ STOPPED ]"
fi
#return $pid_found
}
check_pidfile() {
# Check if File Exists
pidfile_exists=false
if [ -f "$pidfile" ]; then
pidfile_exists=true
#echo ' PID File Found'
#else
#echo ' PID File Not Found'
fi
# Check if File Empty
pidfile_empty=true
if $pidfile_exists; then
get_pidfile
# If file not empty = true
# TODO: Not sure this condition is working
if [ -n "$pid" ]; then
pidfile_empty=false
#echo ' PID File Healthy'
#else
#echo ' PID File Empty'
fi
fi
}
run_program() {
echo " Starting $servicename..."
ensure_piddir
#local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
#su -c "$CMD" $RUNAS > "$PIDFILE"
su -c $program &> $logfile & echo $! $user > $pidfile
read pid < $pidfile
sleep 1
# Check pid Running
check_pidproc
}
check_stopped() {
echo " Stopping $servicename $pid..."
sleep 5
# Get child pid/s
#childpid=
# Wait for Parent pid to stop
#while [ -n $(ps -ef | grep $(cat "$pidfile") | grep "$program" | grep -v grep | grep -v bash) || $count -gt 60 ]
#do
# sleep 1
# count=$[$count+1]
#done
if [ $? -eq 0 ]; then
echo ' Service Stopped [ OK ]'
else
return_value=1
fi
#echo ' Service Stopped [ OK ]' >&2
}
start() {
echo 'Starting Service...'
# Check if pid File Exists and/or Empty
check_pidfile
# File Found and Not Empty
if $pidfile_exists && ! $pidfile_empty; then
get_pidfile
check_pidproc
if $pid_found; then
echo " $servicename $pid Already Running"
else
echo ' Process not currently running'
run_program
fi
# File Found, but Empty
elif $pidfile_exists && $pidfile_empty; then
search_pidproc
if ! $pid_running; then
run_program
else
echo ' Process Already Running'
echo ' Service Start [ FAILED ]'
return_value=1
fi
# File not found
else
search_pidproc
if ! $pid_running; then
run_program
else
echo ' Process Already Running'
echo ' Service Start [ FAILED ]'
return_value=1
fi
fi
return $return_value
}
stop() {
# TODO: Stop all pids running if multiple?
echo 'Stopping Service…'
# Try pid File First
check_pidfile
if $pidfile_exists && ! $pidfile_empty; then
# Pull pid from File
get_pidfile
else
# Look for running pid
search_pidproc
fi
check_pidproc
if $pid_found; then
# TODO: If kill fails because wrong PID in file PIDFile not removed.
kill $pid && rm -f "$pidfile"
check_stopped
fi
return $return_value
}
status() {
# First try "ps"
echo 'Checking Service...'
check_pidfile
if $pidfile_exists && ! $pidfile_empty; then
# Pull pid from File
# TODO: IF PIDFile has wrong PID this fails
get_pidfile
else
# Look for running pid
search_pidproc
fi
check_pidproc
return $return_value
}
case "$1" in
start)
start
return_value=$?
;;
stop)
stop
return_value=$?
;;
status)
status
return_value=$?
;;
restart|reload)
stop
start
return_value=$?
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
return_value=2
esac
exit $return_value