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.33 1999/12/15 18:41:07 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
30 echo -ne "\033[0;3${2}m"
49 awk "BEGIN {printf \"$text\", \"$m\"; }"
52 # National language support function
55 if [ -x /bin/gettext ] || [ -x /usr/bin/gettext ]; then
56 if [ -z "$NLS_DOMAIN" ]; then
57 NLS_DOMAIN="rc-scripts"
59 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$1"`"
68 # Some functions to handle PLD-style messages
71 what="`nls "DONE"`"; typeset -i offset=${#what}
72 text="`nls "$*"`.................................................................................."
73 awk "BEGIN {printf \"%.$((69 - $offset))s\", \"$text\";}"
78 echo -n "`termput setaf 6`[`termput setaf 5` `nls "BUSY"` `termput setaf 6`]`termput setaf 7`"
83 echo "`termput setaf 6`[`termput setaf 2` `nls "DONE"` `termput setaf 6`]`termput setaf 7`"
88 echo "`termput setaf 6`[`termput setaf 2` `nls "WORK"` `termput setaf 6`]`termput setaf 7`"
93 echo "`termput setaf 6`[`termput setaf 1` `nls "FAIL"` `termput setaf 6`]`termput setaf 7`"
98 echo "`termput setaf 6`[`termput setaf 1` `nls "DIED"` `termput setaf 6`]`termput setaf 7`"
103 echo -ne "`nls "\b\b\b\b\b\b\b\b"`"
106 # Usage run_cmd Message command_to_run
111 show "$MESSAGE"; busy
113 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
116 deltext; fail; echo $_ERRORS
123 # A function to start a program (now it's usefull on read-only filesystem too)
128 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
131 '') echo '$0: Usage: daemon [+/-nicelevel] {program}'
133 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
137 # make sure it doesn't core dump anywhere; while this could mask
138 # problems with the daemon, it also closes some security problems
143 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
154 # A function to stop a program.
159 nls "Usage: killproc {program} [signal]"
166 # check for second arg to be kill level
167 if [ "$2" != "" ] ; then
178 pid=`pidofproc $base`
181 if [ "$pid" != "" ] ; then
182 if [ "$notset" = "1" ] ; then
183 if ps h $pid>/dev/null 2>&1; then
184 # TERM first, then KILL if not dead
185 kill -TERM $pid >/dev/null 2>&1
187 if ps h $pid >/dev/null 2>&1 ; then
189 if ps h $pid >/dev/null 2>&1 ; then
191 if ps h $pid >/dev/null 2>&1 ; then
192 kill -KILL $pid >/dev/null 2>&1
196 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
200 # use specified level only
202 if ps h $pid >/dev/null 2>&1; then
203 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
213 # Remove pid file if any.
214 if [ "$notset" = "1" ]; then
215 rm -f /var/run/$base.pid
219 # A function to find the pid of a program.
224 nls "Usage: pidofproc {program}\n"
228 # First try "/var/run/*.pid" files
229 if [ -f /var/run/$1.pid ] ; then
230 pid=`head -1 /var/run/$1.pid`
231 if [ "$pid" != "" ] ; then
239 if [ "$pid" != "" ] ; then
244 # Finally try to extract it from ps
245 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
246 { if ((prog == $5) || (("(" prog ")") == $5) ||
247 (("[" prog "]") == $5) ||
248 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
255 nls "Usage: status {program}\n"
261 if [ "$pid" != "" ] ; then
262 nls "%s (pid %s) is running..." "$1" "$pid"
265 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
266 { if ((prog == $5) || (("(" prog ")") == $5) ||
267 (("[" prog "]") == $5) ||
268 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
269 if [ "$pid" != "" ] ; then
270 nls "%s (pid %s) is running..." "$1" "$pid"
275 # Next try "/var/run/*.pid" files
276 if [ -f /var/run/$1.pid ] ; then
277 pid=`head -1 /var/run/$1.pid`
278 if [ "$pid" != "" ] ; then
279 nls "%s dead but pid file exists" "$1"
283 # See if /var/lock/subsys/$1 exists
284 if [ -f /var/lock/subsys/$1 ]; then
285 nls "%s dead but subsys locked" "$1"
288 nls "%s is stopped" "$1"