Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 97810f467f
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 43 lines (37 sloc) 0.915 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#!/bin/bash

me=`basename $0`

usage () {
    cat <<EOF >&2
Usage: $me <proc> [<lsof_args>]
proc is substring to pass to pgrep
EOF
    exit 1
}

if [ "$1" == '-h' ] || [ "$1" == '--help' ] || [ -z "$1" ]; then
usage
fi

proc="$1"
shift
if [ -e "/proc/$proc" ]; then
pid=$proc
    lsof -p $pid "$@"
else
if pgrep "$proc" >/dev/null; then
pgrep_args=
    else
echo "pgrep \"$proc\" didn't find process; trying pgrep -f ..."
        pgrep_args=-f
    fi

pgrep $pgrep_args "$proc" | while read pid; do
        # Ugh. This is the only way to get the pid of the subshell.
        # $$ returns the parent pid.
        sub_pid=`sh -c 'echo $PPID'`
        if [ "$pid" == $$ ]; then
            : #echo " skipping top lsofp process"
        elif [ "$pid" == $sub_pid ]; then
            : #echo " skipping lsofp subshell"
        else
lsof -p $pid "$@"
        fi
done
fi
Something went wrong with that request. Please try again.