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

Bryan O'Neal Bryan.ONeal at TheONealAndAssociates.com
Thu Feb 4 20:30:05 MST 2016


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 at 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 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.phxlinux.org/pipermail/plug-discuss/attachments/20160204/ef8fd604/attachment.html>


More information about the PLUG-discuss mailing list