I'm on RHEL 5 and tried /proc like this:
$echo $$
50040
$ls -ld /proc/self
lrwxrwxrwx 1 root root 64 Jan 22 15:25 /proc/self -> 22485
I expected that /proc/self link to a subdirectory of /proc that's my current process id. But $$ shows process id is 50040, not 22485. Why is that?
The PID you are seeing when issuing the ls
command is the one for the ls
command, not that of your shell. If you do it many times in a row, you will see it is different each time:
fred> ls -ld /proc/self
lrwxrwxrwx 1 root root 0 Jan 12 13:13 /proc/self -> 5075
fred> ls -ld /proc/self
lrwxrwxrwx 1 root root 0 Jan 12 13:13 /proc/self -> 5076
fred> ls -ld /proc/self
lrwxrwxrwx 1 root root 0 Jan 12 13:13 /proc/self -> 5077
If you want to get the PID of the shell, you need to make sure you use /proc/self
when the shell is running, not one of its sub-processes:
cd /proc/self ; pid=$(awk '{print $1}' stat) ; cd -
(the cd
is a bash
internal command so, at the point where you access /proc/self
, you're still running in the shell process itself).