Continuing the drama of little snippets of shell

| No Comments
This little piece of shell script attempts to find the 'lowest subprocess' of a passed in process. It works well with a straight tree of processes, and if there are the occasional pipes in the command tree then it will miss them most of the time.
Maybe tomorrow I'll talk about how to fix it.
function find_lowest_subprocess() {
        local -i parent=$1
        local pids
        typeset -a pids

        pids=$(pgrep -P $parent)
        while [[ -n "$pids" ]]; do
                if (( ${#pids[@]} > 1 )); then
                        local i=0
                        while (( i < ${#pids[@]} )); do
                                local sub=$(pgrep -P ${pids[$i]})
                                [[ -n $sub ]] && parent=$sub
                                ((i=i + 1))
                        done
                else
                        parent=$pids
                fi
                pids=$(pgrep -P $parent)
        done
        echo $parent
}

Leave a comment

About this Entry

This page contains a single entry by Pete Shanahan published on January 17, 2007 10:32 AM.

Don't just install everything into /usr/local was the previous entry in this blog.

A little poem that was written onto my phone is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.