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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
#*******************************************************************************
# Function - start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args]
#
# Purpose: This runs the specified program as a daemon
#
# Inputs: -f, run the program even if it is already running
# -n nicelevel, specifies a nice level. See nice(1).
# -p pidfile, uses the specified pidfile
# pathname, pathname to the specified program
# args, arguments to pass to specified program
#
# Outputs: return 0 - Success
# return 2 - Invalid or excessive number of arguments,
# warning in stdout
# return 4 - Program or service status is unknown
#
# Dependencies: nice
#
# Todo: none
#
#*******************************************************************************
start_daemon()
{
local pidfile=""
local forcestart=""
local nicelevel="0"
while true
do
case "${1}" in
-f)
forcestart="1"
shift 1
;;
-n)
nicelevel="${2}"
shift 2
;;
-p)
pidfile="${2}"
shift 2
;;
-*)
log_failure_msg "Unknown Option: ${1}"
return 2
;;
*)
break
;;
esac
done
if [ -z "${forcestart}" ]; then
if [ -z "${pidfile}" ]; then
pidofproc "${1}" > /dev/null
else
pidofproc -p "${pidfile}" "${1}" > /dev/null
fi
case "${?}" in
0)
log_warning_msg "Unable to continue: ${1} is running"
return 4
;;
1)
log_warning_msg "Unable to continue: ${pidfile} exists"
return 4
;;
3)
;;
*)
log_failure_msg "Unknown error code from pidofproc: ${?}"
return 4
;;
esac
fi
nice -n "${nicelevel}" "${@}"
}
#*******************************************************************************
# Function - killproc [-p pidfile] pathname [signal]
#
# Purpose:
#
# Inputs: -p pidfile, uses the specified pidfile
# pathname, pathname to the specified program
# signal, send this signal to pathname
#
# Outputs: return 0 - Success
# return 1 - Invalid or excessive number of arguments,
# warning in stdout
# return 4 - Unknown Status
#
# Dependencies: kill
#
# Todo: test
#
#*******************************************************************************
killproc()
{
local pidfile=""
local killsig=""
local pidlist=""
while true
do
case "${1}" in
-p)
pidfile="${2}"
shift 2
;;
-*)
log_failure_msg "Unknown Option: ${1}"
return 1
;;
*)
break
;;
esac
done
if [ "${#}" = "2" ]; then
killsig="${2}"
elif [ "${#}" != "1" ]; then
shift 2
log_failure_msg "Excess Arguments: $@"
return 1
fi
if [ -z "${pidfile}" ]; then
pidlist=`pidofproc "${1}"`
else
pidlist=`pidofproc -p "${pidfile}" "${1}"`
fi
for pid in ${pidlist}
do
kill -${killsig:-TERM} ${pid} 2> /dev/null
if [ -z "${killsig}" ]; then
# Wait up to 3 seconds, for ${pid} to terminate
local dtime=3
while [ "${dtime}" != "0" ]
do
kill -0 ${pid} 2> /dev/null || break
sleep 1
dtime=$(( ${dtime} - 1))
done
# If ${pid} is still running, kill it
kill -0 ${pid} 2> /dev/null && kill -KILL ${pid} 2> /dev/null
fi
done
if [ -z "${killsig}" ]; then
pidofproc "${1}" 2>&1 > /dev/null
# Program was terminated
if [ "$?" != "0" ]; then
# Pidfile Exists
if [ -f "${pidfile}" ]; then
rm -f "${pidfile}" 2>&1 > /dev/null
fi
return 0
else # Program is still running
return 4 # Unknown Status
fi
else
if [ -z "${pidfile}" ]; then
pidofproc "${1}" 2> /dev/null
else
pidofproc -p "${pidfile}" "${1}" 2> /dev/null
fi
fi
}
#*******************************************************************************
# Function - pidofproc [-p pidfile] pathname
#
# Purpose: This function returns one or more pid(s) for a particular daemon
#
# Inputs: -p pidfile, use the specified pidfile instead of pidof
# pathname, path to the specified program
#
# Outputs: return 0 - Success, pid's in stdout
# return 1 - Invalid or excessive number of arguments,
# warning in stdout
# return 1 - Program is dead, pidfile exists
# return 3 - Program is not running
#
# Dependencies: pidof, echo
#
# Todo: - Invalid or excessive argments, and program is dead pidfile exists
# conflict with eachother
#
#*******************************************************************************
pidofproc()
{
local pidfile=""
local lpids=""
local pidlist=""
while true
do
case "${1}" in
-p)
pidfile="${2}"
shift 2
;;
-*)
log_failure_msg "Unknown Option: ${1}"
return 1
;;
*)
break
;;
esac
done
if [ "${#}" != "1" ]; then
shift 1
log_failure_msg "Excess Arguments: $@"
return 1
fi
if [ -n "${pidfile}" ]; then
if [ ! -r "${pidfile}" ]; then
return 3 # Program is not running
fi
lpids=`head -n 1 ${pidfile}`
for pid in ${lpids}
do
if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
kill -0 "${pid}" 2> /dev/null &&
pidlist="${pidlist} ${pid}"
fi
echo ${pidlist}
test -z "${pidlist}" && return 1 # Program is dead, pidfile exists
return 0
done
else
pidof "${1}"
fi
if [ "$?" != "0" ]; then
return 3 # Program is not running
fi
}
# Screen Dimentions
if [ -z "${COLUMNS}" ]; then
COLUMNS=$(stty size)
COLUMNS=${COLUMNS##* }
fi
# When using remote connections, such as a serial port, stty size returns 0
if [ "${COLUMNS}" = "0" ]; then
COLUMNS=80
fi
# Measurements for positioning result messages
COL=$((${COLUMNS} - 8))
WCOL=$((${COL} - 2))
# Set Cursur Position Commands, used via echo -e
SET_COL="\\033[${COL}G" # at the $COL char
SET_WCOL="\\033[${WCOL}G" # at the $WCOL char
CURS_UP="\\033[1A\\033[0G" # Up one line, at the 0'th char
# Set color commands, used via echo -e
# Please consult `man console_codes` for more information
# under the "Set Graphics Resolution" section
#
# Warning, when switching from a 8bit to a 9bit font,
# the linux console will reinterpret the bold (1;) to
# the top 256 glyphs of the 9bit font. This does
# not affect framebuffer consoles
NORMAL="\\033[0;39m" # Standard console grey
SUCCESS="\\033[1;32m" # Success is green
WARNING="\\033[1;33m" # Warnings are yellow
FAILURE="\\033[1;31m" # Failures are red
INFO="\\033[1;36m" # Information is light cyan
BRACKET="\\033[1;34m" # Brackets are blue
BOOTMESG_PREFIX=" * " # Text at the beginning of every line
#*******************************************************************************
# Function - log_success_msg "message"
#
# Purpose: Print a success message
#
# Inputs:
#
# Outputs:
#
# Dependencies: echo
#
# Todo: logging
#
#*******************************************************************************
log_success_msg()
{
echo -n -e "${BOOTMESG_PREFIX}${@}"
echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
return 0
}
#*******************************************************************************
# Function - log_failure_msg "message"
#
# Purpose: Print a failure message
#
# Inputs: $@ - Message
#
# Outputs: Text output to screen
#
# Dependencies: echo
#
# Todo: logging
#
#*******************************************************************************
log_failure_msg() {
echo -n -e "${BOOTMESG_PREFIX}${@}"
echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
return 0
}
#*******************************************************************************
# Function - log_warning_msg "message"
#
# Purpose: print a warning message
#
# Inputs: $@ - Message
#
# Outputs: Text output to screen
#
# Dependencies: echo
#
# Todo: logging
#
#*******************************************************************************
log_warning_msg() {
echo -n -e "${BOOTMESG_PREFIX}${@}"
echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
return 0
}
|