2

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.

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 also kill the child process?

0

2 Answers 2

1

Use a negative PID which will cause a process group to be killed. The -- tells kill that the rest of the arguments are not option switches so the hyphen (minus) before the PID won't confuse it.

kill -- -12345

Also, -9 is a last resort. Don't use it until you've tried at least -15 (SIGTERM, which is the default) first. This gives a program the chance to do housekeeping before it exits. See When should I use kill -9 or Useless use of kill -9 or kill -9.

0

killall might give you better results.

Warning: killall behaves differently on the different *nix's. On Linux, it will kill the processes with the titles matching the argument. On Solaris, it will kill EVERY process, not just the ones you specify. So on Solaris, running killall as root will effectively shut down the machine. Make sure you know which behavior the command follows

3
  • 4
    I'd warn, killall behaves differently on the different *nix's. On Linux, it will kill the processes with the titles matching the argument. On Solaris, it will kill EVERY process, not just the ones you specify. So on Solaris, running killall as root will effectively shut down the machine. Make sure you know which behavior the command follows.
    – Babu
    Jan 4, 2010 at 19:37
  • Thanks for this comment, @Babu, this is definitely something i overlooked. Jan 4, 2010 at 19:41
  • 1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .