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.42 2000/03/09 11:43:07 zagrodzki 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"
18 # Source configuration if available - may override default values
19 [ -f /etc/sysconfig/system ] && . /etc/sysconfig/system
21 [ -z "$COLUMNS" ] && COLUMNS=80
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}
100 awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
101 # move to column number $INIT_COL
102 # termput hpa $INIT_COL
107 termput hpa $INIT_COL
108 echo -n "`termput setaf 6`[`termput setaf 5` `nls --nls-domain rc-scripts "BUSY"` `termput setaf 6`]`termput setaf 7`"
113 termput hpa $INIT_COL
114 echo "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "DONE"` `termput setaf 6`]`termput setaf 7`"
119 termput hpa $INIT_COL
120 echo "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "WORK"` `termput setaf 6`]`termput setaf 7`"
125 termput hpa $INIT_COL
126 echo "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "FAIL"` `termput setaf 6`]`termput setaf 7`"
131 termput hpa $INIT_COL
132 echo "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "DIED"` `termput setaf 6`]`termput setaf 7`"
137 echo -ne "`nls "\b\b\b\b\b\b\b\b"`"
140 # Usage run_cmd Message command_to_run
146 show "$MESSAGE"; busy
148 if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
151 deltext; fail; [ -n "$_ERRORS" ] && echo $_ERRORS
158 # compatibility functions
163 run_cmd "$STRING" "$*"
166 # A function to start a program (now it's usefull on read-only filesystem too)
172 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
175 '') nls "Usage: daemon [+/-nicelevel] {program}\n"
177 -*|+*) SERVICE_RUN_NICE_LEVEL=$1
181 # make sure it doesn't core dump anywhere; while this could mask
182 # problems with the daemon, it also closes some security problems
187 if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
194 [ -n "$_ERRORS" ] && echo $_ERRORS
200 # A function to stop a program.
205 nls "Usage: killproc {program} [signal]\n"
212 # check for second arg to be kill level
213 if [ "$2" != "" ] ; then
224 pid=`pidofproc $base`
227 if [ "$pid" != "" ] ; then
228 if [ "$notset" = "1" ] ; then
229 if ps h $pid>/dev/null 2>&1; then
230 # TERM first, then KILL if not dead
231 kill -TERM $pid >/dev/null 2>&1
233 if ps h $pid >/dev/null 2>&1 ; then
235 if ps h $pid >/dev/null 2>&1 ; then
237 if ps h $pid >/dev/null 2>&1 ; then
238 kill -KILL $pid >/dev/null 2>&1
242 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
246 # use specified level only
248 if ps h $pid >/dev/null 2>&1; then
249 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
259 # Remove pid file if any.
260 if [ "$notset" = "1" ]; then
261 rm -f /var/run/$base.pid
265 # A function to find the pid of a program.
270 nls "Usage: pidofproc {program}\n"
274 # First try "/var/run/*.pid" files
275 if [ -f /var/run/$1.pid ] ; then
276 pid=`head -1 /var/run/$1.pid`
277 if [ "$pid" != "" ] ; then
285 if [ "$pid" != "" ] ; then
290 # Finally try to extract it from ps
291 ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
292 { if ((prog == $5) || (("(" prog ")") == $5) ||
293 (("[" prog "]") == $5) ||
294 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
301 nls "Usage: status {program}\n"
307 if [ "$pid" != "" ] ; then
308 nls "%s (pid %s) is running...\n" "$1" "$pid"
311 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
312 { if ((prog == $5) || (("(" prog ")") == $5) ||
313 (("[" prog "]") == $5) ||
314 ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
315 if [ "$pid" != "" ] ; then
316 nls "%s (pid %s) is running...\n" "$1" "$pid"
321 # Next try "/var/run/*.pid" files
322 if [ -f /var/run/$1.pid ] ; then
323 pid=`head -1 /var/run/$1.pid`
324 if [ "$pid" != "" ] ; then
325 nls "%s dead but pid file exists\n" "$1"
329 # See if /var/lock/subsys/$1 exists
330 if [ -f /var/lock/subsys/$1 ]; then
331 nls "%s dead but subsys locked\n" "$1"
334 nls "%s is stopped\n" "$1"
338 # Confirm whether we really want to run this service
340 echo -n "`nls "Start service"` $1 `nls "(Y)es/(N)o/(C)ontinue? [Y] "`"
363 nls "Usage: is_yes {value}\n"
371 [ "$1" = "true" ] ||\
372 [ "$1" = "True" ] ||\
373 [ "$1" = "TRUE" ] ||\
383 nls "Usage: is_no {value}\n"
391 [ "$1" = "false" ] ||\
392 [ "$1" = "False" ] ||\
393 [ "$1" = "FALSE" ] ||\
402 nls "ERROR: Networking is down. %s can't be run.\n" "$1"
407 show "Starting %s service" "$1"
410 msg_Already_Running()
412 nls "%s service is already running.\n" "$1"
417 show "Stopping %s service" "$1"
422 nls "%s service is not running.\n" "$1"
427 show "Reloading %s service" "$1"
432 nls "Usage: %s {start|stop|status|restart|reload|force-reload}\n" "$1"
437 nls "Usage: %s {start|stop|status|restart|force-reload}\n" "$1"