#! /bin/sh # # functions This file contains functions to be used by most or all # shell scripts in the /etc/init.d directory. # # Version: @(#) /etc/init.d/functions 1.01 26-Oct-1993 # # Author: Miquel van Smoorenburg, # Hacked by: Greg Galloway and Marc Ewing # # First set up a default search path. export PATH="/sbin:/usr/sbin:/bin:/usr/bin" # Customize for local use: : set ${LOG_TTY:=/dev/tty12} to where you want messages sent -- should match syslog.conf SKIP_AWK=true # VERBOSE_BOOTUP=true # uncomment to make bootup stuff echo to screen DAEMON_LOG=/var/log/init_daemons # set local3.info to go here in /etc/syslog.conf : ${DAEMON_LOG:-/var/log/messages} default set here if not set already # Put the following at the end of /etc/RC.D/rc # ${VERBOSE_BOOTUP:-echo "Quiet bootup: see $DAEMON_LOG for deamon startup messages."} FAILED_DAEMON_MSG="\nWARNING: some daemon(s) failed to start: see the log file!!\n" # Use ONLY for machines with no monitor (serial terminal control) # SERIAL_CONSOLE=/dev/ttyS0 # set -x MyTTY=`/usr/bin/tty` case "$MyTTY" in /dev/tty?|/dev/conso*|/dev/tty) # exec < $NEWTTY > $NEWTTY 2> $NEWTTY unset SERIAL_CONSOLE ;; not*) : ;; /dev/ttyS?) SERIAL_CONSOLE=/dev/ttyS0 ;; esac set +x # I note that 'logger' and 'basename' are in /usr/bin, and may not be available # on a small root only filesystem... # basename can be replaced with a (limited) shell function: basename(){ builtin echo "${1##*/}" ; } # A function to start a program. daemon() { # Test syntax. if [ $# = 0 ]; then echo "Usage: daemon {program}" return 1 fi # Save basename. base=`basename $1` # See if it's already running. [ "`pidofproc $base`" != "" ] && return # And start it up (with error logging): [ "${VERBOSE_BOOTUP}" ] || builtin echo -n . run_prog "$@" } run_prog(){ base=`basename $1` "$@" || if [ "`pidofproc $base`" = "" ] ; then shift VERBOSE_BOOTUP=error Logger "${ST_MSG:=Start of} $base $* *** FAILED ****" touch /tmp/FAILED_DAEMON_MARKER return 1 fi shift Logger "${ST_MSG:=Started} $base $*" } echo(){ # Converts messages of the form: echo -n "Starting XXX: " for the Logger function # or alternately: echo -n "Shutting down XXX services: " if [ "$#" = 0 -a "$ECHO" ]; then # for the terminating echo in converted statements # Do only if logger ran (ST_MSG is reset): [ "$ST_MSG" = ' ' ] && builtin echo unset ST_MSG ECHO LOG_MSG #elif [ "X${1}" = X-n -a \( "X${2#Shutting}" != "X$2" -o "X${2#Starting}" != "X$2" \) ] elif [ "X${1}" = X-n ] ; then case "$2" in Shutting*|Starting*|Stopping*) ST_MSG="$2" ECHO="builtin echo" # save it for "Logger" function ;; # For normal echo statements: *) builtin echo "$@" ;; esac else # For normal echo statements: if [ "$SERIAL_CONSOLE" ] ; then builtin echo "$@" > $SERIAL_CONSOLE else builtin echo "$@" fi fi } Logger(){ declare LOGGER_TAG="-t daemon" if [ "$ECHO" -a "${VERBOSE_BOOTUP}" ]; then # For old message compatibility: builtin echo -n "$@" # unset LOG_MSG logger -p local3.info $LOGGER_TAG "${LOG_MSG}$*" [ "$ST_MSG" = ' ' ] || LOG_MSG="$ST_MSG " ST_MSG=' ' else # builtin echo actual mesg is: $* logger -p local3.info $LOGGER_TAG ${VERBOSE_BOOTUP:+-s} "$*" unset ST_MSG fi no_syslog_echo "$*" } no_syslog_echo(){ # logging doesn't work until syslogd is running, so we do it "manually" [ -f /var/lock/subsys/syslog ] && return 0 builtin echo "syslog off: $*" >> /var/log/init_daemons builtin echo "syslog off: $*" >> ${LOG_TTY:=/dev/tty12} return 1 } # A function to stop a program. killproc() { # Test syntax. if [ $# = 0 ]; then echo "Usage: killproc {program}" return 1 fi # Save basename. base=`basename $1` # Find pid. pid=`pidofproc $base` # Kill it. if [ "$pid" != "" ] ; then if kill -9 $pid then Logger "${ST_MSG:=Killed} $base" else Logger "Kill of $base *** FAILED ****" fi fi # Remove pid file if any. rm -f /var/run/$base.pid } # A function to find the pid of a program. pidofproc() { # Test syntax. if [ $# = 0 ] ; then echo "Usage: pidofproc {program}" return 1 fi # First try "pidof" type pidof > /dev/null 2>&1 && { pid=`pidof $1` if [ "$pid" != "" ] ; then builtin echo $pid return 0 fi } # Next try "/var/run/*.pid" files type head > /dev/null 2>&1 && if [ -f /var/run/$1.pid ] ; then pid=`head -1 /var/run/$1.pid` if [ "$pid" != "" ] ; then builtin echo $pid return 0 fi fi # To speed things up: [ "$SKIP_AWK" ] && return type awk > /dev/null 2>&1 || return type ps > /dev/null 2>&1 || return # Finally try to extract it from ps ps auxw | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } { if ((prog == $11) || (("(" prog ")") == $11) || ((prog ":") == $11)) { print $2 } }' $1 }