0

Let's say I have for the following simple bash script: test_bg.sh

#!/bin/bash
ping www.google.ro &> /dev/null &

If I execute it directly via ./test_bg.sh and do a ps -ef after, I see that the process ping www.google.ro is inherited by systemd (PID 1), which means that its parent exited, not waiting for it to finish, so I am getting an orphan process:

UID          PID    PPID    PGID     SID  C STIME TTY          TIME CMD
root           1       0       1       1  0 Jan07 ?        00:00:21 /usr/lib/systemd/systemd --system --deserialize 21
...
...
...
nimus     461772       1  461771  458695  0 18:59 pts/1    00:00:00 ping www.google.ro

This, of course happens if I use /bin/bash -c "ping www.google.ro &> /dev/null &" too.

If I execute the code in the script without &, so the code looking like this in a test_fg.sh script:

#!/bin/bash
ping www.google.ro &> /dev/null 

the ps -efj --forest would look like this:

UID          PID    PPID    PGID     SID  C STIME TTY          TIME CMD
nimus     458695  458596  458695  458695  0 17:02 pts/1    00:00:00  |       \_ -bash
nimus     461907  458695  461907  458695  0 19:02 pts/1    00:00:00  |           \_ /bin/bash ./test_fg.sh
nimus     461908  461907  461907  458695  0 19:02 pts/1    00:00:00  |               \_ ping www.google.ro

So the forked process /bin/bash ./test_fg.sh that further fork() again and exec() the ping www.google.ro is still alive.

And, of course executing ping www.google.ro &> /dev/null & directly from my main bash process that is the Session Leader I would get:

[nimus@localhost ~]$ ping www.google.ro &> /dev/null &
[1] 462091
[nimus@localhost ~]$ ps -efj --forest
UID          PID    PPID    PGID     SID  C STIME TTY          TIME CMD
root      458581  351997  458581  458581  0 17:02 ?        00:00:00  \_ sshd: nimus [priv]
nimus     458596  458581  458581  458581  0 17:02 ?        00:00:00  |   \_ sshd: nimus@pts/1
nimus     458695  458596  458695  458695  0 17:02 pts/1    00:00:00  |       \_ -bash
nimus     462091  458695  462091  458695  0 19:10 pts/1    00:00:00  |           \_ ping www.google.ro

So, why does the ping www.google.ro process ends up inherited by Systemd PID 1 when it is executed in background & in script?

1
  • because you're not waiting for it. – Uncle Billy Jan 19 at 17:23
0

When you start a process in the background (using &), it starts executing concurrently with its parent. Since your script does not have anything else to do, it exits and the orphaned process is adopted by init. When you do not use &, the parent waits for the child to finish, suspending itself. and you see the script as parent. In the latter case, parent will show up as suspended/sleeping.

0

The process running the script exits, because it reaches the end of the script. It reaches the end of the script, because the command is run in the background with &.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.