Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID, the PID of its parent, and the command line used to run the parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID
echo "Parent command: $(ps -o cmd= -q $PPID)"

Now, let's run it:

$ bash s1.sh
s1=17955
s2=17956 PPID=17955
Parent command: bash s1.sh

As you can see the second script does, in fact, know the PID of its parent. Using ps, that PID reveals the command line used to invoke the parent.

For a discussion of PPID in more depth, see Stéphane Chazelas's answeranswer.

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID, the PID of its parent, and the command line used to run the parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID
echo "Parent command: $(ps -o cmd= -q $PPID)"

Now, let's run it:

$ bash s1.sh
s1=17955
s2=17956 PPID=17955
Parent command: bash s1.sh

As you can see the second script does, in fact, know the PID of its parent. Using ps, that PID reveals the command line used to invoke the parent.

For a discussion of PPID in more depth, see Stéphane Chazelas's answer.

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID, the PID of its parent, and the command line used to run the parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID
echo "Parent command: $(ps -o cmd= -q $PPID)"

Now, let's run it:

$ bash s1.sh
s1=17955
s2=17956 PPID=17955
Parent command: bash s1.sh

As you can see the second script does, in fact, know the PID of its parent. Using ps, that PID reveals the command line used to invoke the parent.

For a discussion of PPID in more depth, see Stéphane Chazelas's answer.

added 123 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID, the PID of its parent, and the command line used to run the parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID
echo "Parent command: $(ps -o cmd= -q $PPID)"

Now, let's run it:

$ bash s1.sh
s1=17955
s2=17956 PPID=17955
Parent command: bash s1.sh

As you can see the second script does, in fact, know the PID of its parent. Using ps, that PID reveals the command line used to invoke the parent.

For a discussion of PPID in more depth, see Stéphane Chazelas's answer.

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID, the PID of its parent, and the command line used to run the parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID
echo "Parent command: $(ps -o cmd= -q $PPID)"

Now, let's run it:

$ bash s1.sh
s1=17955
s2=17956 PPID=17955
Parent command: bash s1.sh

As you can see the second script does, in fact, know the PID of its parent. Using ps, that PID reveals the command line used to invoke the parent.

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID, the PID of its parent, and the command line used to run the parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID
echo "Parent command: $(ps -o cmd= -q $PPID)"

Now, let's run it:

$ bash s1.sh
s1=17955
s2=17956 PPID=17955
Parent command: bash s1.sh

As you can see the second script does, in fact, know the PID of its parent. Using ps, that PID reveals the command line used to invoke the parent.

For a discussion of PPID in more depth, see Stéphane Chazelas's answer.

added 108 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID and that, the PID of its parent, and the command line used to run the parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID
echo "Parent command: $(ps -o cmd= -q $PPID)"

Now, let's run it:

$ bash s1.sh
s1=15827s1=17955
s2=15828s2=17956 PPID=15827PPID=17955
Parent command: bash s1.sh

As you can see the second script does, in fact, know the PID of its parent.

Still more information on the parent is available from Using ps, that PID reveals the command line used to invoke the parent. For example:

ps -fq $PPID

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID and that of its parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID

Now, let's run it:

$ bash s1.sh
s1=15827
s2=15828 PPID=15827

As you can see the second script does, in fact, know the PID of its parent.

Still more information on the parent is available from ps. For example:

ps -fq $PPID

Yes, a program can know who its parent is.

To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script:

$ cat s1.sh
#!/bin/bash
echo s1=$$
bash s2.sh

The second script reports its process ID, the PID of its parent, and the command line used to run the parent:

$ cat s2.sh
#!/bin/bash
echo s2=$$ PPID=$PPID
echo "Parent command: $(ps -o cmd= -q $PPID)"

Now, let's run it:

$ bash s1.sh
s1=17955
s2=17956 PPID=17955
Parent command: bash s1.sh

As you can see the second script does, in fact, know the PID of its parent. Using ps, that PID reveals the command line used to invoke the parent.

added 98 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
Loading
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
Loading