How does killproc knows what PID to kill

init.d

When I look at my /etc/rc.d/init.d/functions, I see the following under my killproc() function.

        if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
                RC=7 # Program is not running
        else
                failure $"$base shutdown"
                RC=0
        fi

The problem is that I have to set my /etc/init.d/<prog> script to call with the following.

killproc -p /jail/var/run/prog.pid

Instead of the following.

killproc prog

When I trace through the killproc() function it brought me to the above if statement, whenever I don't supply the -p <PID file> option. Can someone let me know what that if statement does as I don't know what the LSB is for.

Best Answer

killprocneeds the full file path of the program to kill, not just the program name. It preforms some sort of dictionary lookup and matches the full program path to the PID to kill.

LSB stands for Linux Standards Base. It's designed to help maximise interoperability between different distros. However, CentOS ignores the LSB in places, particularly here (according to the standard, /etc/init.d/functions should be located in /lib, and should offer a start-daemon function, among other things).

Looking through the killproc source code, it seems that, if there's no pid file specified, killproc searches through functions in the init.d, looking for a match to the program name specified. If it doesn't find it, it falls through to the if statement you traced to.

If you look at the crond init script (which is pretty well written and helpfully clear about what's going on), it shows the proper invocation of killproc

#note the full path to script 
 exec=/usr/sbin/crond
 stop() {
     if [ $UID -ne 0 ] ; then
         echo "User has insufficient privilege."
         exit 4
     fi
     echo -n $"Stopping $prog: "
     if [ -n "`pidfileofproc $exec`" ]; then
         killproc $exec
     RETVAL=3
     else
         failure $"Stopping $prog"
     fi
     retval=$?
     echo
     [ $retval -eq 0 ] && rm -f $lockfile
    }
Related Question