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.36 2000/02/18 17:59:19 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 -o -x /usr/bin/gettext ]; then
69 OLD_NLS_DOMAIN="$NLS_DOMAIN"
70 if [ "$1" = "--nls-domain" ]; then
75 if [ -z "$NLS_DOMAIN" ]; then
76 NLS_DOMAIN="rc-scripts"
79 # avoid translating empty text. --misiek
80 if [ -n "$MESSAGE" ]; then
81 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$MESSAGE"`"
88 NLS_DOMAIN="$OLD_NLS_DOMAIN"
94 # Some functions to handle PLD-style messages
97 what="`nls --nls-domain rc-scripts "DONE"`"; typeset -i offset=${#what}
98 text="`nls "$*"`.................................................................................."
99 awk "BEGIN {printf \"%.$((75 - $offset))s\", \"$text\";}"
100 # move to column number 67
106 echo -n "`termput setaf 6`[`termput setaf 5` `nls --nls-domain rc-scripts "BUSY"` `termput setaf 6`]`termput setaf 7`"
111 echo "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "DONE"` `termput setaf 6`]`termput setaf 7`"
116 echo "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "WORK"` `termput setaf 6`]`termput setaf 7`"
121 echo "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "FAIL"` `termput setaf 6`]`termput setaf 7`"
126 echo "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "DIED"` `termput setaf 6`]`termput setaf 7`"
131 echo -ne "`nls --nls-domain rc-scripts "\b\b\b\b\b\b\b\b"`"
134 # Usage run_cmd Message command_to_run
140 show "$MESSAGE"; busy
142 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
145 deltext; fail; [ -n "$_ERRORS" ] && echo $_ERRORS
152 # compatibility functions
157 run_cmd "$STRING" "$*"
160 # A function to start a program (now it's usefull on read-only filesystem too)
166 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
169 '') echo '$0: Usage: daemon [+/-nicelevel] {program}'
171 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
175 # make sure it doesn't core dump anywhere; while this could mask
176 # problems with the daemon, it also closes some security problems
181 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
188 [ -n "$_ERRORS" ] && echo $_ERRORS
194 # A function to stop a program.
199 nls "Usage: killproc {program} [signal]"
206 # check for second arg to be kill level
207 if [ "$2" != "" ] ; then
218 pid=`pidofproc $base`
221 if [ "$pid" != "" ] ; then
222 if [ "$notset" = "1" ] ; then
223 if ps h $pid>/dev/null 2>&1; then
224 # TERM first, then KILL if not dead
225 kill -TERM $pid >/dev/null 2>&1
227 if ps h $pid >/dev/null 2>&1 ; then
229 if ps h $pid >/dev/null 2>&1 ; then
231 if ps h $pid >/dev/null 2>&1 ; then
232 kill -KILL $pid >/dev/null 2>&1
236 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
240 # use specified level only
242 if ps h $pid >/dev/null 2>&1; then
243 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
253 # Remove pid file if any.
254 if [ "$notset" = "1" ]; then
255 rm -f /var/run/$base.pid
259 # A function to find the pid of a program.
264 nls "Usage: pidofproc {program}\n"
268 # First try "/var/run/*.pid" files
269 if [ -f /var/run/$1.pid ] ; then
270 pid=`head -1 /var/run/$1.pid`
271 if [ "$pid" != "" ] ; then
279 if [ "$pid" != "" ] ; then
284 # Finally try to extract it from ps
285 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
286 { if ((prog == $5) || (("(" prog ")") == $5) ||
287 (("[" prog "]") == $5) ||
288 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
295 nls "Usage: status {program}\n"
301 if [ "$pid" != "" ] ; then
302 nls "%s (pid %s) is running..." "$1" "$pid"
305 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
306 { if ((prog == $5) || (("(" prog ")") == $5) ||
307 (("[" prog "]") == $5) ||
308 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
309 if [ "$pid" != "" ] ; then
310 nls "%s (pid %s) is running..." "$1" "$pid"
315 # Next try "/var/run/*.pid" files
316 if [ -f /var/run/$1.pid ] ; then
317 pid=`head -1 /var/run/$1.pid`
318 if [ "$pid" != "" ] ; then
319 nls "%s dead but pid file exists" "$1"
323 # See if /var/lock/subsys/$1 exists
324 if [ -f /var/lock/subsys/$1 ]; then
325 nls "%s dead but subsys locked" "$1"
328 nls "%s is stopped" "$1"