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.21 1999/08/14 11:01:57 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"
38 # National language support function
41 if [ -x /bin/gettext ] || [ -x /usr/bin/gettext ]; then
42 if [ -z "$NLS_DOMAIN" ]; then
43 NLS_DOMAIN="rc-scripts"
45 TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$@"
52 # Some functions to handle PLD-style messages
55 what="`nls "DONE"`"; typeset -i offset=${#what}
56 text="`nls "$*"`"..................................................................................
57 awk "BEGIN {printf \"%.$((69 - $offset))s\", \"$text\";}"
62 echo -n "`tput setaf 6`[`tput setaf 5` `nls "BUSY"` `tput setaf 6`]`tput setaf 7`"
67 echo "`tput setaf 6`[`tput setaf 2` `nls "DONE"` `tput setaf 6`]`tput setaf 7`"
72 echo "`tput setaf 6`[`tput setaf 2` `nls "WORK"` `tput setaf 6`]`tput setaf 7`"
77 echo "`tput setaf 6`[`tput setaf 1` `nls "FAIL"` `tput setaf 6`]`tput setaf 7`"
82 echo "`tput setaf 6`[`tput setaf 1` `nls "DIED"` `tput setaf 6`]`tput setaf 7`"
87 echo -ne "`nls '\b\b\b\b\b\b\b\b'`"
90 # Usage run_cmd Message command_to_run
97 if _ERRORS="`initlog -c \"$*\"`"; then
100 deltext; fail; echo $_ERRORS
107 # A function to start a program (now it's usefull on read-only filesystem too)
112 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
115 '') echo '$0: Usage: daemon [+/-nicelevel] {program}'
117 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
121 # make sure it doesn't core dump anywhere; while this could mask
122 # problems with the daemon, it also closes some security problems
127 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
138 # A function to stop a program.
143 nls "Usage: killproc {program} [signal]"
150 # check for second arg to be kill level
151 if [ "$2" != "" ] ; then
162 pid=`pidofproc $base`
165 if [ "$pid" != "" ] ; then
166 if [ "$notset" == "1" ] ; then
167 if ps h $pid>/dev/null 2>&1; then
168 # TERM first, then KILL if not dead
169 kill -TERM $pid >/dev/null 2>&1
171 if ps h $pid >/dev/null 2>&1 ; then
173 if ps h $pid >/dev/null 2>&1 ; then
175 if ps h $pid >/dev/null 2>&1 ; then
176 kill -KILL $pid >/dev/null 2>&1
181 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
182 # use specified level only
184 if ps h $pid >/dev/null 2>&1; then
185 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
195 # Remove pid file if any.
196 if [ "$notset" = "1" ]; then
197 rm -f /var/run/$base.pid
201 # A function to find the pid of a program.
206 nls "Usage: pidofproc {program}\n"
210 # First try "/var/run/*.pid" files
211 if [ -f /var/run/$1.pid ] ; then
212 pid=`head -1 /var/run/$1.pid`
213 if [ "$pid" != "" ] ; then
221 if [ "$pid" != "" ] ; then
226 # Finally try to extract it from ps
227 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
228 { if ((prog == $5) || (("(" prog ")") == $5) ||
229 (("[" prog "]") == $5) ||
230 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
237 nls "Usage: status {program}\n"
243 if [ "$pid" != "" ] ; then
244 nls "$1 (pid $pid) is running..."
247 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
248 { if ((prog == $5) || (("(" prog ")") == $5) ||
249 (("[" prog "]") == $5) ||
250 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
251 if [ "$pid" != "" ] ; then
252 nls "$1 (pid $pid) is running..."
257 # Next try "/var/run/*.pid" files
258 if [ -f /var/run/$1.pid ] ; then
259 pid=`head -1 /var/run/$1.pid`
260 if [ "$pid" != "" ] ; then
261 nls "$1 dead but pid file exists"
265 # See if /var/lock/subsys/$1 exists
266 if [ -f /var/lock/subsys/$1 ]; then
267 nls "$1 dead but subsys locked"