Louis Ng
Louis Ng

Reputation: 553

Why processes run from a shell script has parent id 1

running the following command would spawn a python process with parent pid 1

echo "python3 -m http.server 2>&1 &" > a && chmod 777 a && ./a && ps -ef | grep "python3 -m http.server"

result

  501  4622     1   0  4:45PM ttys000    0:00.00 python3 -m http.server

while running the following

python3 -m http.server 2>&1 &
ps -ef | grep "python3 -m http.server"

will have something different

501  4646   665   0  4:51PM ttys000    0:00.07 python3 -m http.server

Can anyone explain?

Upvotes: 0

Views: 1133

Answers (1)

Bob Goddard
Bob Goddard

Reputation: 977

All processes must have a parent. If a parent spawns a child, then the parent exits, the child must still have parent. When this happens, its parent is set to the init process, which has an pid of 1.

As for why, the above 2 cases are different... The second has its parent set to the shell pid, and the shell has not yet exited.

As for the first, you are spawning it from a shell, the server is put into the background by the use of '&', the shell then exits. We then go through paragraph 1.

Upvotes: 3

Related Questions