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

Top Page
Attachments:
Message as email
+ (text/plain)
+ (text/plain)
Delete this message
Reply to this message
Author: der.hans
Date:  
To: Main PLUG discussion list
CC: Arizona State University Linux Users Group
Subject: Re: BASH - How to get the pid of a backgrounded process from the process
Am 04. Feb, 2016 schwätzte Bryan O'Neal so:

moin moin,

$( ) is command redirection.

( ) is a subshell, sort of.

Here are some excerpts from the bash man page:

###
        (list) list is executed in a subshell environment (see  COMMAND EXECU
               TION  ENVIRONMENT below).


COMMAND EXECUTION ENVIRONMENT
        The shell has an execution environment, which consists of  the
follow
        ing:
.
.
.
        ·      various  process  IDs,  including  those of background
jobs, the
               value of $$, and the value of PPID
###


If you want a full subshell, invoke bash and create another process. Also,
check the values of SHLVL.

:) lufthans@fred:~$ cat fred.sh 
function backGround() {
         echo "Pid $1 is $$ $SHLVL"
         sleep 3
}
function backGround_subshell() {
         echo "echo Pid $1 is $$ $SHLVL"
         sleep 3
}
echo "My pid is $$ $SHLVL"
backGround 1 &
echo "BPid 1 is $!"
ps -ef | grep $!
(backGround_subshell 4) &
echo "BPid 4 is $!"
ps -ef | grep $!
bash -c 'echo true subshell PID $$ $SHLVL; ps -ef | grep $$'
wait
echo
:) lufthans@fred:~$


:) lufthans@fred:~$ bash fred.sh anke
My pid is 27695 2
BPid 1 is 27696
Pid 1 is 27695 2
lufthans 27696 27695  0 21:26 pts/0    00:00:00 bash fred.sh anke
lufthans 27698 27695  0 21:26 pts/0    00:00:00 grep 27696
lufthans 27699 27696  0 21:26 pts/0    00:00:00 sleep 3
BPid 4 is 27700
echo Pid 4 is 27695 2
lufthans 27700 27695  0 21:26 pts/0    00:00:00 bash fred.sh anke
lufthans 27702 27695  0 21:26 pts/0    00:00:00 grep 27700
true subshell PID 27704 3
lufthans 27704 27695  0 21:26 pts/0    00:00:00 bash -c echo true subshell
PID $$ $SHLVL; ps -ef | grep $$
lufthans 27705 27704  0 21:26 pts/0    00:00:00 ps -ef
lufthans 27706 27704  0 21:26 pts/0    00:00:00 grep 27704


:) lufthans@fred:~$

ciao,

der.hans

> 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

>


-- 
#  http://www.LuftHans.com/        http://www.PhxLinux.org/
#  Practice socially conscious hedonism. Do whatever you want,
#  as long as it doesn't hurt anyone else. - der.hans
---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change your mail settings:
http://lists.phxlinux.org/mailman/listinfo/plug-discuss