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.sh
would invoke child.sh
Here is my script:
Now I run bash father.sh
, you could check it ps -alf
Then I killed the father.sh
by kill -9 24588
, and I guessed the child process should be terminated but unfortunately I was wrong.
Could anyone explain why?
thx
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).
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.
An orphan process in OS is one which is executing but it's parent process has terminated is called an orphan process.
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).
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).
-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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With