BASH - How to get the pid of a backgrounded process from the…

Top Page
Attachments:
Message as email
+ (text/plain)
+ (text/html)
+ (text/plain)
Delete this message
Reply to this message
Author: Bryan O'Neal
Date:  
To: Arizona State University Linux Users Group, Main PLUG discussion list
Subject: BASH - How to get the pid of a backgrounded process from the process
Sorry for the cross post but this is not an easy question I don't think....

Given:
Pid of a process is $$
Pid of a child process just spawned is $!
However a child process does not see $$ as it's pid but rather the pid of
it's parent... How can the child process know it's own pid?

Example:
[root@mybox l]# cat backTest
function backGround() {
        echo "Pid $1 is $$"
}
function backGround_subshell() {
        echo "echo Pid $1 is $$"
}
echo "My pid is $$"
backGround 1 &
echo "BPid 1 is $!"
backGround 2 &
echo "BPid 2 is $!"
backGround 3 &
echo "BPid 3 is $!"
$(backGround_subshell 4) &
echo "BPid 4 is $!"
$(backGround_subshell 5) &
echo "BPid 5 is $!"
$(backGround_subshell 6) &
echo "BPid 6 is $!"
wait
echo
[root@mybox l]# bash backTest
My pid is 16712
BPid 1 is 16713
Pid 1 is 16712
BPid 2 is 16714
Pid 2 is 16712
BPid 3 is 16715
BPid 4 is 16716
BPid 5 is 16717
BPid 6 is 16719
Pid 4 is 16712
Pid 3 is 16712
Pid 6 is 16712
Pid 5 is 16712

---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change your mail settings:
http://lists.phxlinux.org/mailman/listinfo/plug-discuss