Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why child process still alive after parent process was killed in Linux?

Someone told me that when you killed a parent process in linux, the child would die.
But I doubt it. So I wrote two bash scripts, where father.shwould invoke child.sh

Here is my script:

enter image description here

Now I run bash father.sh, you could check it ps -alf enter image description here

Then I killed the father.sh by kill -9 24588, and I guessed the child process should be terminated but unfortunately I was wrong. enter image description here

Could anyone explain why?

thx

like image 495
爱国者 Avatar asked Dec 16 '11 11:12

爱国者


People also ask

What happens to child process when parent is killed Linux?

Orphan Processes When a parent process dies before a child process, the kernel knows that it's not going to get a wait call, so instead it makes these processes "orphans" and puts them under the care of init (remember mother of all processes).

Can child process exist without parent process?

A child process may also be known as subprocess or a subtask. A child process is created as a copy of its parent process. The child process inherits most of its attributes. If a child process has no parent process, then the child process is created directly by the kernel.

When a parent process dies What is the child process called?

An orphan process in OS is one which is executing but it's parent process has terminated is called an orphan process.

What happens if the parent process is killed first?

No. If the parent is killed, children become children of the init process (that has the process id 1 and is launched as the first user process by the kernel). The init process checks periodically for new children, and waits for them (thus freeing resources that are allocated by their return value).


2 Answers

No, when you kill a process alone, it will not kill the children.

You have to send the signal to the process group if you want all processes for a given group to receive the signal

For example, if your parent process id has the code 1234, you will have to specify the parentpid adding the symbol minus followed by your parent process id:

kill -9 -1234 

Otherwise, orphans will be linked to init, as shown by your third screenshot (PPID of the child has become 1).

like image 78
fge Avatar answered Sep 16 '22 14:09

fge


-bash: kill: (-123) - No such process

In an interactive Terminal.app session the foreground process group id number and background process group id number are different by design when job control/monitor mode is enabled. In other words, if you background a command in a job-control enabled Terminal.app session, the $! pid of the backgrounded process is in fact a new process group id number (pgid).

In a script having no job control enabled, however, this may not be the case! The pid of the backgrounded process may not be a new pgid but a normal pid! And this is, what causes the error message -bash: kill: (-123) - No such process, trying to kill a process group but only specifying a normal pid (instead of a pgid) to the kill command.

# the following code works in Terminal.app because $! == $pgid { sleep 100 & IFS=" " read -r pgid <<EOF $(ps -p $! -o pgid=) EOF echo $$ $! $pgid sleep 10 kill -HUP -- -$! #kill -HUP --  -${pgid}  # use in script } 
like image 43
charz Avatar answered Sep 17 '22 14:09

charz