Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am running a bash script as a background job. The bash script calls a time-consuming executable. If I am not wrong, the running of the bash script is the parent process and the running of the executable is the child process.

(1) when I look at the output of top, it only shows the child process which is the running of the executable, not shows the parent process which is the running of the script. Just wonder why not show both? And how to show both?

(2) I now want to stop the whole running by killing the parent process which is the background job

kill -9 $(jobs -p)

The terminal shows that the running of the bash script is killed. But the running of the executable still hangs on the output of top. I just wonder how to kill both parent and child process?

Thanks and regards!

share|improve this question
    
I find a lot of people just think "kill -9" when they think "kill" -- what happens if you kill, but not kill -9 the parent? Will it perhaps send the correct signals down the chain? –  Michael Graff Jan 4 '10 at 19:58
add comment

2 Answers

up vote 1 down vote accepted

Use a negative PID to kill a process group. Try to avoid -9 unless absolutely necessary.

share|improve this answer
    
Could you be more specific about "useing a negative PID to kill a process group" for my parent and child processes case? What is preferred over -9? –  Tim Jan 4 '10 at 20:27
1  
just plain kill is preferred - it gives the process a chance to clean up before it closes. kill -9 is a last resort for when the app isn't responding to a plain kill –  James Polley Jan 4 '10 at 21:05
1  
Plain kill sends SIGHUP, which is signal 15 - so kill -15 PID is equivalent to kill PID. negative PID just means to put a minus-sign in front of the pid - so in your example, kill -15 -$(jobs -p) –  James Polley Jan 4 '10 at 21:17
    
@Tim: James Polley is correct. You can also use kill -- -$(jobs -p). You should be aware that if there are more jobs than just one, these commands will kill all of them (but only the first one will have a negative PID). You can use a jobspec to select a particular job. As far as top goes, does your script background the executable? If not, top should show both, unless it's getting lost in the noise. Try top -u username to narrow things down a little. –  Dennis Williamson Jan 4 '10 at 22:52
add comment

The bash process should still show up in the process list, except that since it usually wouldn't be doing anything in this situation, it would probably be at the very bottom of the "top" list. Try ps or better yet pstree to analyze your situation.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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