1 # functions This file contains functions to be used by most or all
2 # shell scripts in the /etc/init.d directory.
4 # $Id: functions,v 1.35 2000/02/03 11:49:04 misiek Exp $
6 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by: Greg Galloway and Marc Ewing
9 # Marek Obuchowicz <elephant@pld.org.pl>
10 # Arkadiusz Mi¶kiewicz <misiek@pld.org.pl>
12 # First set up a default search path.
13 export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
15 if [ -f /etc/sysconfig/system ]; then
16 . /etc/sysconfig/system
19 if [ -z "$COLUMNS" ]; then
26 if [ "$COLOR_INIT" = "no" ]; then
28 elif [ ! -d /usr/share/terminfo ] || \
29 ! ( [ -x /usr/bin/tput ] || [ -x /bin/tput ] ) ; then
41 echo -ne "\033[0;3${2}m"
62 awk "BEGIN {printf \"$text\", \"$m\"; }"
65 # National language support function
68 if [ -x /bin/gettext ] || [ -x /usr/bin/gettext ]; then
69 if [ -z "$NLS_DOMAIN" ]; then
70 NLS_DOMAIN="rc-scripts"
73 # avoid translating empty text. --misiek
74 if [ -n "$MESSAGE" ]; then
75 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$MESSAGE"`"
87 # Some functions to handle PLD-style messages
90 what="`nls "DONE"`"; typeset -i offset=${#what}
91 text="`nls "$*"`.................................................................................."
92 awk "BEGIN {printf \"%.$((75 - $offset))s\", \"$text\";}"
93 # move to column number 67
99 echo -n "`termput setaf 6`[`termput setaf 5` `nls "BUSY"` `termput setaf 6`]`termput setaf 7`"
104 echo "`termput setaf 6`[`termput setaf 2` `nls "DONE"` `termput setaf 6`]`termput setaf 7`"
109 echo "`termput setaf 6`[`termput setaf 2` `nls "WORK"` `termput setaf 6`]`termput setaf 7`"
114 echo "`termput setaf 6`[`termput setaf 1` `nls "FAIL"` `termput setaf 6`]`termput setaf 7`"
119 echo "`termput setaf 6`[`termput setaf 1` `nls "DIED"` `termput setaf 6`]`termput setaf 7`"
124 echo -ne "`nls "\b\b\b\b\b\b\b\b"`"
127 # Usage run_cmd Message command_to_run
133 show "$MESSAGE"; busy
135 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
138 deltext; fail; [ -n "$_ERRORS" ] && echo $_ERRORS
145 # compatibility functions
150 run_cmd "$STRING" "$*"
153 # A function to start a program (now it's usefull on read-only filesystem too)
159 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
162 '') echo '$0: Usage: daemon [+/-nicelevel] {program}'
164 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
168 # make sure it doesn't core dump anywhere; while this could mask
169 # problems with the daemon, it also closes some security problems
174 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
181 [ -n "$_ERRORS" ] && echo $_ERRORS
187 # A function to stop a program.
192 nls "Usage: killproc {program} [signal]"
199 # check for second arg to be kill level
200 if [ "$2" != "" ] ; then
211 pid=`pidofproc $base`
214 if [ "$pid" != "" ] ; then
215 if [ "$notset" = "1" ] ; then
216 if ps h $pid>/dev/null 2>&1; then
217 # TERM first, then KILL if not dead
218 kill -TERM $pid >/dev/null 2>&1
220 if ps h $pid >/dev/null 2>&1 ; then
222 if ps h $pid >/dev/null 2>&1 ; then
224 if ps h $pid >/dev/null 2>&1 ; then
225 kill -KILL $pid >/dev/null 2>&1
229 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
233 # use specified level only
235 if ps h $pid >/dev/null 2>&1; then
236 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
246 # Remove pid file if any.
247 if [ "$notset" = "1" ]; then
248 rm -f /var/run/$base.pid
252 # A function to find the pid of a program.
257 nls "Usage: pidofproc {program}\n"
261 # First try "/var/run/*.pid" files
262 if [ -f /var/run/$1.pid ] ; then
263 pid=`head -1 /var/run/$1.pid`
264 if [ "$pid" != "" ] ; then
272 if [ "$pid" != "" ] ; then
277 # Finally try to extract it from ps
278 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
279 { if ((prog == $5) || (("(" prog ")") == $5) ||
280 (("[" prog "]") == $5) ||
281 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
288 nls "Usage: status {program}\n"
294 if [ "$pid" != "" ] ; then
295 nls "%s (pid %s) is running..." "$1" "$pid"
298 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
299 { if ((prog == $5) || (("(" prog ")") == $5) ||
300 (("[" prog "]") == $5) ||
301 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
302 if [ "$pid" != "" ] ; then
303 nls "%s (pid %s) is running..." "$1" "$pid"
308 # Next try "/var/run/*.pid" files
309 if [ -f /var/run/$1.pid ] ; then
310 pid=`head -1 /var/run/$1.pid`
311 if [ "$pid" != "" ] ; then
312 nls "%s dead but pid file exists" "$1"
316 # See if /var/lock/subsys/$1 exists
317 if [ -f /var/lock/subsys/$1 ]; then
318 nls "%s dead but subsys locked" "$1"
321 nls "%s is stopped" "$1"