2001-07-10 14:25:50 +00:00
|
|
|
#!/bin/bash
|
2006-12-20 21:11:55 +00:00
|
|
|
# pid-identifier.sh:
|
|
|
|
# Gives complete path name to process associated with pid.
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
ARGNO=1 # Number of arguments the script expects.
|
|
|
|
E_WRONGARGS=65
|
|
|
|
E_BADPID=66
|
|
|
|
E_NOSUCHPROCESS=67
|
|
|
|
E_NOPERMISSION=68
|
|
|
|
PROCFILE=exe
|
|
|
|
|
|
|
|
if [ $# -ne $ARGNO ]
|
|
|
|
then
|
2014-07-05 13:38:40 +00:00
|
|
|
echo "Usage: `basename $0` PID-number" >&2 # Error message >stderr.
|
2001-07-10 14:25:50 +00:00
|
|
|
exit $E_WRONGARGS
|
|
|
|
fi
|
|
|
|
|
|
|
|
pidno=$( ps ax | grep $1 | awk '{ print $1 }' | grep $1 )
|
|
|
|
# Checks for pid in "ps" listing, field #1.
|
|
|
|
# Then makes sure it is the actual process, not the process invoked by this script.
|
|
|
|
# The last "grep $1" filters out this possibility.
|
2005-08-28 18:12:12 +00:00
|
|
|
#
|
|
|
|
# pidno=$( ps ax | awk '{ print $1 }' | grep $1 )
|
|
|
|
# also works, as Teemu Huovila, points out.
|
|
|
|
|
2006-12-20 21:11:55 +00:00
|
|
|
if [ -z "$pidno" ] # If, after all the filtering, the result is a zero-length string,
|
|
|
|
then #+ no running process corresponds to the pid given.
|
2001-07-10 14:25:50 +00:00
|
|
|
echo "No such process running."
|
|
|
|
exit $E_NOSUCHPROCESS
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Alternatively:
|
2014-07-05 13:38:40 +00:00
|
|
|
# if ! ps $1 > /dev/null 2>&1
|
2001-07-10 14:25:50 +00:00
|
|
|
# then # no running process corresponds to the pid given.
|
|
|
|
# echo "No such process running."
|
|
|
|
# exit $E_NOSUCHPROCESS
|
|
|
|
# fi
|
|
|
|
|
2001-10-15 14:21:41 +00:00
|
|
|
# To simplify the entire process, use "pidof".
|
|
|
|
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
if [ ! -r "/proc/$1/$PROCFILE" ] # Check for read permission.
|
|
|
|
then
|
|
|
|
echo "Process $1 running, but..."
|
|
|
|
echo "Can't get read permission on /proc/$1/$PROCFILE."
|
|
|
|
exit $E_NOPERMISSION # Ordinary user can't access some files in /proc.
|
|
|
|
fi
|
|
|
|
|
|
|
|
# The last two tests may be replaced by:
|
2014-07-05 13:38:40 +00:00
|
|
|
# if ! kill -0 $1 > /dev/null 2>&1 # '0' is not a signal, but
|
2001-07-10 14:25:50 +00:00
|
|
|
# this will test whether it is possible
|
|
|
|
# to send a signal to the process.
|
2014-07-05 13:38:40 +00:00
|
|
|
# then echo "PID doesn't exist or you're not its owner" >&2
|
2001-07-10 14:25:50 +00:00
|
|
|
# exit $E_BADPID
|
|
|
|
# fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exe_file=$( ls -l /proc/$1 | grep "exe" | awk '{ print $11 }' )
|
|
|
|
# Or exe_file=$( ls -l /proc/$1/exe | awk '{print $11}' )
|
|
|
|
#
|
2006-12-20 21:11:55 +00:00
|
|
|
# /proc/pid-number/exe is a symbolic link
|
|
|
|
#+ to the complete path name of the invoking process.
|
2001-07-10 14:25:50 +00:00
|
|
|
|
2006-12-20 21:11:55 +00:00
|
|
|
if [ -e "$exe_file" ] # If /proc/pid-number/exe exists,
|
|
|
|
then #+ then the corresponding process exists.
|
2001-07-10 14:25:50 +00:00
|
|
|
echo "Process #$1 invoked by $exe_file."
|
|
|
|
else
|
|
|
|
echo "No such process running."
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2006-12-20 21:11:55 +00:00
|
|
|
# This elaborate script can *almost* be replaced by
|
|
|
|
# ps ax | grep $1 | awk '{ print $5 }'
|
|
|
|
# However, this will not work...
|
|
|
|
#+ because the fifth field of 'ps' is argv[0] of the process,
|
|
|
|
#+ not the executable file path.
|
2001-07-10 14:25:50 +00:00
|
|
|
#
|
|
|
|
# However, either of the following would work.
|
|
|
|
# find /proc/$1/exe -printf '%l\n'
|
|
|
|
# lsof -aFn -p $1 -d txt | sed -ne 's/^n//p'
|
|
|
|
|
|
|
|
# Additional commentary by Stephane Chazelas.
|
|
|
|
|
|
|
|
exit 0
|