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!

  • 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

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

  • 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. – Paused until further notice. Jan 4 '10 at 22:52

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.

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.