| 1 | #!/bin/bash |
| 2 | # pid-identifier.sh: |
| 3 | # Gives complete path name to process associated with pid. |
| 4 | |
| 5 | ARGNO=1 # Number of arguments the script expects. |
| 6 | E_WRONGARGS=65 |
| 7 | E_BADPID=66 |
| 8 | E_NOSUCHPROCESS=67 |
| 9 | E_NOPERMISSION=68 |
| 10 | PROCFILE=exe |
| 11 | |
| 12 | if [ $# -ne $ARGNO ] |
| 13 | then |
| 14 | echo "Usage: `basename $0` PID-number" >&2 # Error message >stderr. |
| 15 | exit $E_WRONGARGS |
| 16 | fi |
| 17 | |
| 18 | pidno=$( ps ax | grep $1 | awk '{ print $1 }' | grep $1 ) |
| 19 | # Checks for pid in "ps" listing, field #1. |
| 20 | # Then makes sure it is the actual process, not the process invoked by this script. |
| 21 | # The last "grep $1" filters out this possibility. |
| 22 | # |
| 23 | # pidno=$( ps ax | awk '{ print $1 }' | grep $1 ) |
| 24 | # also works, as Teemu Huovila, points out. |
| 25 | |
| 26 | if [ -z "$pidno" ] # If, after all the filtering, the result is a zero-length string, |
| 27 | then #+ no running process corresponds to the pid given. |
| 28 | echo "No such process running." |
| 29 | exit $E_NOSUCHPROCESS |
| 30 | fi |
| 31 | |
| 32 | # Alternatively: |
| 33 | # if ! ps $1 > /dev/null 2>&1 |
| 34 | # then # no running process corresponds to the pid given. |
| 35 | # echo "No such process running." |
| 36 | # exit $E_NOSUCHPROCESS |
| 37 | # fi |
| 38 | |
| 39 | # To simplify the entire process, use "pidof". |
| 40 | |
| 41 | |
| 42 | if [ ! -r "/proc/$1/$PROCFILE" ] # Check for read permission. |
| 43 | then |
| 44 | echo "Process $1 running, but..." |
| 45 | echo "Can't get read permission on /proc/$1/$PROCFILE." |
| 46 | exit $E_NOPERMISSION # Ordinary user can't access some files in /proc. |
| 47 | fi |
| 48 | |
| 49 | # The last two tests may be replaced by: |
| 50 | # if ! kill -0 $1 > /dev/null 2>&1 # '0' is not a signal, but |
| 51 | # this will test whether it is possible |
| 52 | # to send a signal to the process. |
| 53 | # then echo "PID doesn't exist or you're not its owner" >&2 |
| 54 | # exit $E_BADPID |
| 55 | # fi |
| 56 | |
| 57 | |
| 58 | |
| 59 | exe_file=$( ls -l /proc/$1 | grep "exe" | awk '{ print $11 }' ) |
| 60 | # Or exe_file=$( ls -l /proc/$1/exe | awk '{print $11}' ) |
| 61 | # |
| 62 | # /proc/pid-number/exe is a symbolic link |
| 63 | #+ to the complete path name of the invoking process. |
| 64 | |
| 65 | if [ -e "$exe_file" ] # If /proc/pid-number/exe exists, |
| 66 | then #+ then the corresponding process exists. |
| 67 | echo "Process #$1 invoked by $exe_file." |
| 68 | else |
| 69 | echo "No such process running." |
| 70 | fi |
| 71 | |
| 72 | |
| 73 | # This elaborate script can *almost* be replaced by |
| 74 | # ps ax | grep $1 | awk '{ print $5 }' |
| 75 | # However, this will not work... |
| 76 | #+ because the fifth field of 'ps' is argv[0] of the process, |
| 77 | #+ not the executable file path. |
| 78 | # |
| 79 | # However, either of the following would work. |
| 80 | # find /proc/$1/exe -printf '%l\n' |
| 81 | # lsof -aFn -p $1 -d txt | sed -ne 's/^n//p' |
| 82 | |
| 83 | # Additional commentary by Stephane Chazelas. |
| 84 | |
| 85 | exit 0 |