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.26 1999/09/15 21:20:36 wiget 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
20 if [ "$COLOR_INIT" == "no" ]; then
22 elif [ ! -d /usr/share/terminfo ] || \
23 ! ( [ -x /usr/bin/tput ] || [ -x /bin/tput ] ) ; then
24 if [ "$2" == "1" ]; then echo -ne "\033[0;31m"
25 elif [ "$2" == "2" ]; then echo -ne "\033[0;32m"
26 elif [ "$2" == "5" ]; then echo -ne "\033[0;35m"
27 elif [ "$2" == "6" ]; then echo -ne "\033[0;36m"
28 elif [ "$2" == "7" ]; then echo -ne "\033[0;37m"
48 awk "BEGIN {printf \"$text\", \"$m\"; }"
51 # National language support function
54 if [ -x /bin/gettext ] || [ -x /usr/bin/gettext ]; then
55 if [ -z "$NLS_DOMAIN" ]; then
56 NLS_DOMAIN="rc-scripts"
58 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$1"`"
67 # Some functions to handle PLD-style messages
70 what="`nls "DONE"`"; typeset -i offset=${#what}
71 text="`nls "$*"`.................................................................................."
72 awk "BEGIN {printf \"%.$((69 - $offset))s\", \"$text\";}"
77 echo -n "`termput setaf 6`[`termput setaf 5` `nls "BUSY"` `termput setaf 6`]`termput setaf 7`"
82 echo "`termput setaf 6`[`termput setaf 2` `nls "DONE"` `termput setaf 6`]`termput setaf 7`"
87 echo "`termput setaf 6`[`termput setaf 2` `nls "WORK"` `termput setaf 6`]`termput setaf 7`"
92 echo "`termput setaf 6`[`termput setaf 1` `nls "FAIL"` `termput setaf 6`]`termput setaf 7`"
97 echo "`termput setaf 6`[`termput setaf 1` `nls "DIED"` `termput setaf 6`]`termput setaf 7`"
102 echo -ne "`nls "\b\b\b\b\b\b\b\b"`"
105 # Usage run_cmd Message command_to_run
110 show "$MESSAGE"; busy
112 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
115 deltext; fail; echo $_ERRORS
122 # A function to start a program (now it's usefull on read-only filesystem too)
127 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
130 '') echo '$0: Usage: daemon [+/-nicelevel] {program}'
132 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
136 # make sure it doesn't core dump anywhere; while this could mask
137 # problems with the daemon, it also closes some security problems
142 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
153 # A function to stop a program.
158 nls "Usage: killproc {program} [signal]"
165 # check for second arg to be kill level
166 if [ "$2" != "" ] ; then
177 pid=`pidofproc $base`
180 if [ "$pid" != "" ] ; then
181 if [ "$notset" == "1" ] ; then
182 if ps h $pid>/dev/null 2>&1; then
183 # TERM first, then KILL if not dead
184 kill -TERM $pid >/dev/null 2>&1
186 if ps h $pid >/dev/null 2>&1 ; then
188 if ps h $pid >/dev/null 2>&1 ; then
190 if ps h $pid >/dev/null 2>&1 ; then
191 kill -KILL $pid >/dev/null 2>&1
196 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
197 # use specified level only
199 if ps h $pid >/dev/null 2>&1; then
200 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
210 # Remove pid file if any.
211 if [ "$notset" = "1" ]; then
212 rm -f /var/run/$base.pid
216 # A function to find the pid of a program.
221 nls "Usage: pidofproc {program}\n"
225 # First try "/var/run/*.pid" files
226 if [ -f /var/run/$1.pid ] ; then
227 pid=`head -1 /var/run/$1.pid`
228 if [ "$pid" != "" ] ; then
236 if [ "$pid" != "" ] ; then
241 # Finally try to extract it from ps
242 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
243 { if ((prog == $5) || (("(" prog ")") == $5) ||
244 (("[" prog "]") == $5) ||
245 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
252 nls "Usage: status {program}\n"
258 if [ "$pid" != "" ] ; then
259 nls "%s (pid %s) is running..." "$1" "$pid"
262 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
263 { if ((prog == $5) || (("(" prog ")") == $5) ||
264 (("[" prog "]") == $5) ||
265 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
266 if [ "$pid" != "" ] ; then
267 nls "%s (pid %s) is running..." "$1" "$pid"
272 # Next try "/var/run/*.pid" files
273 if [ -f /var/run/$1.pid ] ; then
274 pid=`head -1 /var/run/$1.pid`
275 if [ "$pid" != "" ] ; then
276 nls "%s dead but pid file exists" "$1"
280 # See if /var/lock/subsys/$1 exists
281 if [ -f /var/lock/subsys/$1 ]; then
282 nls "%s dead but subsys locked" "$1"
285 nls "%s is stopped" "$1"