2

I have wrote a shell script names testShell.I run this shell using command:

sudo sh testShell

Then I use ps -ef|grep testShell|grep -v grep

to check the processes created.I find that system have created two processes for me:

imcas@ubuntu:~$ ps -ef|grep testShell|grep -v grep
root      5687 32482  0 11:37 pts/1    00:00:00 sudo sh testShell
root      5688  5687 48 11:37 pts/1    00:00:08 sh testShell

From the ppid,I know that one is parent and the other is child.Now I want to kill the running shell script.What should I do?Just kill the child or should I kill the parent? I find that if I kill the child using kill -9 5688 , both the child and the parent is killed.But if I kill the parent using kill -9 5687 ,just the parent is killed. Anyone could tell me the reason?

3

In Linux, once a parent process is killed its child process becomes an orphan. But then the "warm hearted" init process adopts that orphan process which allows it to proceed.

In order to kill parent and its children processes you can use:

pkill -TERM -P <parent's PID>

(Note: orphan process is different from zombie process, but that's for another question :-)

3

The parent (sudo) is notified that the child process has exited. As the only purpose of the parent was to run this child process it terminates. Other processes would not terminate just because you kill a child process.

On the other hand the child doesn't care what the parent process does. The parent could even terminate immediately after the child process has been created. sudo waits for the child to exit because sudo needs the child's exit code for its own exit code.

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.