-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
319 lines (283 loc) · 6.86 KB
/
functions
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/sh
#
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.
#
# Version: @(#) /etc/init.d/functions 1.01 26-Oct-1993
#
# Author: Miquel van Smoorenburg, <[email protected]>
# Hacked by: Greg Galloway and Marc Ewing
# Karl DeBisschop (19991018 for solaris)
#
# First set up a default search path.
PATH=/opt/gnu/bin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
export PATH
# Get a sane screen width
[ -z "$COLUMNS" ] && COLUMNS=80
# Read in our configuration
if [ -z "$BOOTUP" ]; then
if [ -f /etc/sysconfig/init ]; then
. /etc/sysconfig/init
else
# This all seem confusing? Look in /etc/sysconfig/init,
# or in /usr/doc/initscripts-*/sysconfig.txt
BOOTUP=color
RES_COL=20
MOVE_TO_COL="printf \033[300C\033[%dD ${RES_COL}"
SETCOLOR_SUCCESS='printf \033[1;%dm 32'
SETCOLOR_FAILURE='printf \033[1;%dm 31'
SETCOLOR_WARNING='printf \033[1;%dm 33'
SETCOLOR_NORMAL='printf \033[0;%dm 39'
LOGLEVEL=1
fi
fi
if [ "$BOOTUP" != "verbose" ]; then
INITLOG_CMD="initlog -q -c"
else
INITLOG_CMD="initlog -q -c"
fi
#if which initlog | egrep "^no initlog" >/dev/null 2&1; then
# INITLOG_CMD=""
#fi
#if which initlog | egrep "^no pidof" >/dev/null 2>&1; then
# IN_INITLOG="not found"
#else
# IN_INITLOG=
#fi
# A function to start a program.
daemon() {
# Test syntax.
gotbase=
case $1 in
'') echo '$0: Usage: daemon [+/-nicelevel] {program}'
return 1;;
--check)
shift
base=$1
gotbase="yes"
shift
nicelevel=0
;;
-*|+*) nicelevel=$1
shift;;
*) nicelevel=0;;
esac
# Save basename.
[ -z $gotbase ] && base=`basename $1`
# See if it's already running.
pid=`pidofproc $base`
[ -n "$pid" ] && ps -p $pid >/dev/null 2>&1 && return
# make sure it doesn't core dump anywhere; while this could mask
# problems with the daemon, it also closes some security problems
ulimit -c 0
# Echo daemon
[ "$BOOTUP" = "verbose" ] && printf " $base"
# And start it up.
nice -n $nicelevel $INITLOG_CMD "$*" && success "$base startup" || failure "$base startup"
}
# A function to stop a program.
killproc() {
# Test syntax.
if [ $# = 0 ]; then
echo "Usage: killproc {program} [signal]"
return 1
fi
notset=0
# check for second arg to be kill level
if [ "$2" != "" ] ; then
killlevel=$2
else
notset=1
killlevel="-9"
fi
# Save basename.
base=`basename $1`
# Find pid.
pid=`pidofproc $base`
# Kill it.
if [ "$pid" != "" ] ; then
[ $BOOTUP = "verbose" ] && printf "$base "
if [ "$notset" = "1" ] ; then
if ps -p $pid>/dev/null 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid
sleep 1
if ps -p $pid >/dev/null 2>&1 ; then
sleep 1
if ps -p $pid >/dev/null 2>&1 ; then
sleep 3
if ps -p $pid >/dev/null 2>&1 ; then
kill -KILL $pid
fi
fi
fi
fi
ps -p $pid >/dev/null 2>&1 && failure "$base shutdown" || success "$base shutdown"
# use specified level only
else
if ps -p $pid >/dev/null 2>&1; then
kill $killlevel $pid && success "$base $killlevel" || failure "$base $killlevel"
fi
fi
else
failure "$base shutdown"
fi
# Remove pid file if any.
if [ "$notset" = "1" ]; then
rm -f /var/run/$base.pid
fi
}
# A function to find the pid of a program.
pidofproc() {
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: pidofproc {program}"
return 1
fi
# First try "/var/run/*.pid" files
if [ -f /var/run/$1.pid ] ; then
pid=`head -1 /var/run/$1.pid`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
fi
# Next try "pidof"
# if which pidof | egrep -v "^no pidof" >/dev/null 2>&1; then
# pid=`pidof $1`
# if [ "$pid" != "" ] ; then
# echo $pid
# return 0
# fi
# fi
# Finally try to extract it from ps
pid=`ps -eo pid,ppid,fname | egrep -v $$ | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } { if ((prog == $3) || (("(" prog ")") == $3) || (("[" prog "]") == $3) || ((prog ":") == $3)) { print $1 ; exit 0 } }' $1`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
return 2
}
status() {
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: status {program}"
return 1
fi
base=`basename $1`
pid=`pidofproc $base`
if [ "$pid" != "" ] ; then
echo "$1 (pid $pid) is running..."
return 0
fi
# Next try "/var/run/*.pid" files
if [ -f /var/run/$1.pid ] ; then
pid=`head -1 /var/run/$1.pid`
if [ "$pid" != "" ] ; then
echo "$1 dead but pid file exists"
return 1
fi
fi
# See if /var/lock/subsys/$1 exists
if [ -f /var/lock/subsys/$1 ]; then
echo "$1 dead but subsys locked"
return 2
fi
echo "$1 is stopped"
return 3
}
echo_success() {
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
printf "[ "
[ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
printf "OK"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
printf " ]"
return 0
}
echo_failure() {
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
printf "["
[ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
printf "FAILED"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
printf "]"
return 1
}
echo_passed() {
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
printf "["
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
printf "PASSED"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
printf "]"
return 1
}
# Log that something succeeded
success() {
if [ -z "$IN_INITLOG" ]; then
# initlog -n $0 -s "$1" -e 1
logger -t $0 "Success: $1"
else
logger -t $0 "Success: $1"
# echo "-n $0 -s \"$1\" -e 1" >&21
fi
[ "$BOOTUP" != "verbose" ] && echo_success
return 0
}
# Log that something failed
failure() {
rc=$?
if [ -z "$IN_INITLOG" ]; then
logger -t $0 "Failure: $1"
# initlog -n $0 -s "$1" -e 2
else
logger -t $0 "Failure: $1"
# echo "-n $0 -s \"$1\" -e 2" >&21
fi
[ "$BOOTUP" != "verbose" ] && echo_failure
return $rc
}
# Log that something passed, but may have had errors. Useful for fsck
passed() {
rc=$?
if [ -z "$IN_INITLOG" ]; then
logger -t $0 "Success: $1"
# initlog -n $0 -s "$1" -e 1
else
logger -t $0 "Success: $1"
# echo "-n $0 -s \"$1\" -e 1" >&21
fi
[ "$BOOTUP" != "verbose" ] && echo_passed
return $rc
}
# Run some action. Log its output.
action() {
STRING=$1
printf "$STRING "
shift
$INITLOG_CMD "$*" && success "$STRING" || failure "$STRING"
rc=$?
echo
return $rc
}
# Confirm whether we really want to run this service
confirm() {
printf "Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
read answer
case $answer in
y|Y|"")
return 0
;;
c|C)
return 2
;;
n|N)
return 1
;;
*)
confirm $1
return $?
;;
esac
}