-
Notifications
You must be signed in to change notification settings - Fork 13
/
lsofp
executable file
·42 lines (37 loc) · 915 Bytes
/
lsofp
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