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.23 1999/09/02 12:29:17 misiek Exp $
6 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by: Greg Galloway and Marc Ewing
9 # Marek Obuchowicz <elephant@shadow.eu.org>
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 [ -z "$COLUMNS" ] && COLUMNS=80
19 if [ "$COLOR_INIT" == "no" ]; then
24 elif [ ! -d /usr/share/terminfo ]; then
27 if [ "$2" == "1" ]; then echo -ne "\033[0;31m"
28 elif [ "$2" == "2" ]; then echo -ne "\033[0;32m"
29 elif [ "$2" == "5" ]; then echo -ne "\033[0;35m"
30 elif [ "$2" == "6" ]; then echo -ne "\033[0;36m"
31 elif [ "$2" == "7" ]; then echo -ne "\033[0;37m"
54 awk "BEGIN {printf \"$text\", \"$m\"; }"
57 # National language support function
60 if [ -x /bin/gettext ] || [ -x /usr/bin/gettext ]; then
61 if [ -z "$NLS_DOMAIN" ]; then
62 NLS_DOMAIN="rc-scripts"
64 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$1"`"
73 # Some functions to handle PLD-style messages
76 what="`nls "DONE"`"; typeset -i offset=${#what}
77 text="`nls "$*"`.................................................................................."
78 awk "BEGIN {printf \"%.$((69 - $offset))s\", \"$text\";}"
83 echo -n "`termput setaf 6`[`termput setaf 5` `nls "BUSY"` `termput setaf 6`]`termput setaf 7`"
88 echo "`termput setaf 6`[`termput setaf 2` `nls "DONE"` `termput setaf 6`]`termput setaf 7`"
93 echo "`termput setaf 6`[`termput setaf 2` `nls "WORK"` `termput setaf 6`]`termput setaf 7`"
98 echo "`termput setaf 6`[`termput setaf 1` `nls "FAIL"` `termput setaf 6`]`termput setaf 7`"
103 echo "`termput setaf 6`[`termput setaf 1` `nls "DIED"` `termput setaf 6`]`termput setaf 7`"
108 echo -ne "`nls '\b\b\b\b\b\b\b\b'`"
111 # Usage run_cmd Message command_to_run
116 show "$MESSAGE"; busy
118 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
121 deltext; fail; echo $_ERRORS
128 # A function to start a program (now it's usefull on read-only filesystem too)
133 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
136 '') echo '$0: Usage: daemon [+/-nicelevel] {program}'
138 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
142 # make sure it doesn't core dump anywhere; while this could mask
143 # problems with the daemon, it also closes some security problems
148 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
159 # A function to stop a program.
164 nls "Usage: killproc {program} [signal]"
171 # check for second arg to be kill level
172 if [ "$2" != "" ] ; then
183 pid=`pidofproc $base`
186 if [ "$pid" != "" ] ; then
187 if [ "$notset" == "1" ] ; then
188 if ps h $pid>/dev/null 2>&1; then
189 # TERM first, then KILL if not dead
190 kill -TERM $pid >/dev/null 2>&1
192 if ps h $pid >/dev/null 2>&1 ; then
194 if ps h $pid >/dev/null 2>&1 ; then
196 if ps h $pid >/dev/null 2>&1 ; then
197 kill -KILL $pid >/dev/null 2>&1
202 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
203 # use specified level only
205 if ps h $pid >/dev/null 2>&1; then
206 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
216 # Remove pid file if any.
217 if [ "$notset" = "1" ]; then
218 rm -f /var/run/$base.pid
222 # A function to find the pid of a program.
227 nls "Usage: pidofproc {program}\n"
231 # First try "/var/run/*.pid" files
232 if [ -f /var/run/$1.pid ] ; then
233 pid=`head -1 /var/run/$1.pid`
234 if [ "$pid" != "" ] ; then
242 if [ "$pid" != "" ] ; then
247 # Finally try to extract it from ps
248 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
249 { if ((prog == $5) || (("(" prog ")") == $5) ||
250 (("[" prog "]") == $5) ||
251 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
258 nls "Usage: status {program}\n"
264 if [ "$pid" != "" ] ; then
265 nls "%s (pid %s) is running..." "$1" "$pid"
268 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
269 { if ((prog == $5) || (("(" prog ")") == $5) ||
270 (("[" prog "]") == $5) ||
271 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
272 if [ "$pid" != "" ] ; then
273 nls "%s (pid %s) is running..." "$1" "$pid"
278 # Next try "/var/run/*.pid" files
279 if [ -f /var/run/$1.pid ] ; then
280 pid=`head -1 /var/run/$1.pid`
281 if [ "$pid" != "" ] ; then
282 nls "%s dead but pid file exists" "$1"
286 # See if /var/lock/subsys/$1 exists
287 if [ -f /var/lock/subsys/$1 ]; then
288 nls "%s dead but subsys locked" "$1"
291 nls "%s is stopped" "$1"