2 #*******************************************************************************
3 # Function - start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args]
5 # Purpose: This runs the specified program as a daemon
7 # Inputs: -f, run the program even if it is already running
8 # -n nicelevel, specifies a nice level. See nice(1).
9 # -p pidfile, uses the specified pidfile
10 # pathname, pathname to the specified program
11 # args, arguments to pass to specified program
13 # Outputs: return 0 - Success
14 # return 2 - Invalid or excessive number of arguments,
16 # return 4 - Program or service status is unknown
22 #*******************************************************************************
45 log_failure_msg "Unknown Option: ${1}"
54 if [ -z "${forcestart}" ]; then
55 if [ -z "${pidfile}" ]; then
56 pidofproc "${1}" > /dev/null
58 pidofproc -p "${pidfile}" "${1}" > /dev/null
63 log_warning_msg "Unable to continue: ${1} is running"
67 log_warning_msg "Unable to continue: ${pidfile} exists"
73 log_failure_msg "Unknown error code from pidofproc: ${?}"
79 nice -n "${nicelevel}" "${@}"
82 #*******************************************************************************
83 # Function - killproc [-p pidfile] pathname [signal]
87 # Inputs: -p pidfile, uses the specified pidfile
88 # pathname, pathname to the specified program
89 # signal, send this signal to pathname
91 # Outputs: return 0 - Success
92 # return 1 - Invalid or excessive number of arguments,
94 # return 4 - Unknown Status
100 #*******************************************************************************
114 log_failure_msg "Unknown Option: ${1}"
123 if [ "${#}" = "2" ]; then
125 elif [ "${#}" != "1" ]; then
127 log_failure_msg "Excess Arguments: $@"
131 if [ -z "${pidfile}" ]; then
132 pidlist=`pidofproc "${1}"`
134 pidlist=`pidofproc -p "${pidfile}" "${1}"`
137 for pid in ${pidlist}
139 kill -${killsig:-TERM} ${pid} 2> /dev/null
140 if [ -z "${killsig}" ]; then
141 # Wait up to 3 seconds, for ${pid} to terminate
143 while [ "${dtime}" != "0" ]
145 kill -0 ${pid} 2> /dev/null || break
147 dtime=$(( ${dtime} - 1))
149 # If ${pid} is still running, kill it
150 kill -0 ${pid} 2> /dev/null && kill -KILL ${pid} 2> /dev/null
154 if [ -z "${killsig}" ]; then
155 pidofproc "${1}" 2>&1 > /dev/null
157 # Program was terminated
158 if [ "$?" != "0" ]; then
160 if [ -f "${pidfile}" ]; then
161 rm -f "${pidfile}" 2>&1 > /dev/null
164 else # Program is still running
165 return 4 # Unknown Status
168 if [ -z "${pidfile}" ]; then
169 pidofproc "${1}" 2> /dev/null
171 pidofproc -p "${pidfile}" "${1}" 2> /dev/null
176 #*******************************************************************************
177 # Function - pidofproc [-p pidfile] pathname
179 # Purpose: This function returns one or more pid(s) for a particular daemon
181 # Inputs: -p pidfile, use the specified pidfile instead of pidof
182 # pathname, path to the specified program
184 # Outputs: return 0 - Success, pid's in stdout
185 # return 1 - Invalid or excessive number of arguments,
187 # return 1 - Program is dead, pidfile exists
188 # return 3 - Program is not running
190 # Dependencies: pidof, echo
192 # Todo: - Invalid or excessive argments, and program is dead pidfile exists
193 # conflict with eachother
195 #*******************************************************************************
209 log_failure_msg "Unknown Option: ${1}"
218 if [ "${#}" != "1" ]; then
220 log_failure_msg "Excess Arguments: $@"
224 if [ -n "${pidfile}" ]; then
225 if [ ! -r "${pidfile}" ]; then
226 return 3 # Program is not running
229 lpids=`head -n 1 ${pidfile}`
232 if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
233 kill -0 "${pid}" 2> /dev/null &&
234 pidlist="${pidlist} ${pid}"
237 test -z "${pidlist}" && return 1 # Program is dead, pidfile exists
245 if [ "$?" != "0" ]; then
246 return 3 # Program is not running
251 if [ -z "${COLUMNS}" ]; then
253 COLUMNS=${COLUMNS##* }
256 # When using remote connections, such as a serial port, stty size returns 0
257 if [ "${COLUMNS}" = "0" ]; then
261 # Measurements for positioning result messages
262 COL=$((${COLUMNS} - 8))
265 # Set Cursur Position Commands, used via echo -e
266 SET_COL="\\033[${COL}G" # at the $COL char
267 SET_WCOL="\\033[${WCOL}G" # at the $WCOL char
268 CURS_UP="\\033[1A\\033[0G" # Up one line, at the 0'th char
270 # Set color commands, used via echo -e
271 # Please consult `man console_codes` for more information
272 # under the "Set Graphics Resolution" section
274 # Warning, when switching from a 8bit to a 9bit font,
275 # the linux console will reinterpret the bold (1;) to
276 # the top 256 glyphs of the 9bit font. This does
277 # not affect framebuffer consoles
278 NORMAL="\\033[0;39m" # Standard console grey
279 SUCCESS="\\033[1;32m" # Success is green
280 WARNING="\\033[1;33m" # Warnings are yellow
281 FAILURE="\\033[1;31m" # Failures are red
282 INFO="\\033[1;36m" # Information is light cyan
283 BRACKET="\\033[1;34m" # Brackets are blue
285 BOOTMESG_PREFIX=" * " # Text at the beginning of every line
288 #*******************************************************************************
289 # Function - log_success_msg "message"
291 # Purpose: Print a success message
301 #*******************************************************************************
304 echo -n -e "${BOOTMESG_PREFIX}${@}"
305 echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
309 #*******************************************************************************
310 # Function - log_failure_msg "message"
312 # Purpose: Print a failure message
314 # Inputs: $@ - Message
316 # Outputs: Text output to screen
322 #*******************************************************************************
324 echo -n -e "${BOOTMESG_PREFIX}${@}"
325 echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
329 #*******************************************************************************
330 # Function - log_warning_msg "message"
332 # Purpose: print a warning message
334 # Inputs: $@ - Message
336 # Outputs: Text output to screen
342 #*******************************************************************************
344 echo -n -e "${BOOTMESG_PREFIX}${@}"
345 echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"