# Function:    pidofproc [-p pidfile] pathname
#   The pidofproc function shall return one or more process
#   identifiers for a particular daemon using the algorithm
#   given above. Only process identifiers of running
#   processes should be returned. Multiple process
#   identifiers shall be separated by a single space.
#   The pidofproc function shall return the LSB defined exit
#   status codes for "status". It shall return 0 if the
#   program is running and not 0 otherwise.
#
#   If the status action is requested, the init script will return
#   the following exit status codes.
#   0       program is running or service is OK
#   1       program is dead and /var/run pid file exists
#   2       program is dead and /var/lock lock file exists
#   3       program is not running
#   4       program or service status is unknown
#   5-99    reserved for future LSB use
#   100-149 reserved for distribution use
#   150-199 reserved for application use
#   200-254 reserved
function pidofproc {
    local pidfile=""
    local program=""
    local pid=""
    local pidlist=""
    local list=""
    local exitstatus=0
    # Process arguments
    while [[ $# -gt 0 ]]; do
        case "${1}" in
            -p) shift; pidfile="${1}" ;;
            *)    program="${1##*/}" ;;
        esac
        shift
    done
    # If a PID file is not specified, make one
    test -z "${pidfile}" && pidfile="/var/run/${program}.pid"
    # If a pid file exists and is readable, use it.
    if [[ -r "${pidfile}" ]]; then
        list=$(head -n1 "${pidfile}")
    else
        list=$(pidof "${program}")
        exitstatus=$?
        if [[ ${exitstatus} -gt 0 ]]; then exitstatus=4; fi
    fi
    if [[ exitstatus -eq 0 ]]; then
        # Figure out if all listed PIDs are running.
        for pid in ${list}; do
            if kill -0 "${pid}" 2> /dev/null; then
                pidlist+="${pid} "
            else
                exitstatus=1
            fi
        done
    fi
    if [[ -z "${pidlist}" ]]; then
        if [[ -f "${pidfile}" ]]; then
            exitstatus=1
        else
            exitstatus=3
        fi
    fi
    printf "%s" "${pidlist% }"
    return ${exitstatus}
}


Testing Function: pidofproc: Start
Testing pidofproc: valid program, no pidfile: running
    Returns pid and exit status of 0
    pidofproc /usr/sbin/dhcpcd: ---->:pid:[472] exit status:[0]
    pidofproc dhcpcd: ---->:pid:[472] exit status:[0]

Testing pidofproc: valid program, valid pidfile: running
    Returns pid and exit status of 0
    pidofproc -p /var/run/dhcpcd.pid /usr/sbin/dhcpcd: ---->:pid:[472] exit status:[0]     pidofproc /usr/sbin/dhcpcd -p /var/run/dhcpcd.pid: ---->:pid:[472] exit status:[0]     pidofproc -p /var/run/dhcpcd.pid dhcpcd: ---->:pid:[472] exit status:[0]     pidofproc dhcpcd -p /var/run/dhcpcd.pid: ---->: pid:[472] exit status:[0]

Testing pidofproc: invalid program, no pidfile
    Returns no pid and exit status of 3
    pidofproc /usr/sbin/barf: ---->: pid:[] exit status:[3]

Testing pidofproc: valid program, invalid pidfile: running
    Returns pid and exit status of 0
    pidofproc -p barf.pid /usr/sbin/dhcpcd: ---->: pid:[472] exit status:[0]     pidofproc /usr/sbin/dhcpcd -p barf.pid: ---->: pid:[472] exit status:[0]

Testing pidofproc: valid program, no pidfile: not running
    Returns no pid and exit status of 3
    pidofproc /bin/ls: ---->: pid:[] exit status:[3]

Testing pidofproc: valid program: valid pidfile: not running
    Returns no pid and exit status of 1

touch /var/run/ls.pid

    pidofproc /bin/ls: ---->: pid:[] exit status:[1]
    pidofproc -p /var/run/ls.pid /bin/ls: ---->: pid:[] exit status:[1]
rm /var/run/ls.pid

--
http://lists.linuxfromscratch.org/listinfo/lfs-support
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page

Do not top post on this list.

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

http://en.wikipedia.org/wiki/Posting_style

Reply via email to