How To Kill A Process That Won't Respond To ctrl-C

Sometimes, particularly when writing a bash scripts or one liners, a process is attached to the terminal and cannot be stopped with the usual ctrl-c or ctrl-q.

There are two ways to kill this process. The first is to open another terminal and find the PID of the process with ps or top and then issue the kill command:

kill 7653

However, this involves opening an additional terminal, logging in etc.

Another, faster way to kill a runaway or non-responsive process is to firstly stop and detach it from the terminal:

ctrl-z

This will suspend the process and give you back control of the terminal. Running this command will produce the following output:

[1]+  Stopped                ./rubbish-script.sh

This information tells you that the process has now become a stopped job and the number in square brackets is its job number.

The job can now be killed using the kill command with the option %n where n is the job number. In order to kill the ./rubbish-script.sh process, which is job number 1, we just need to issue the following command:

kill %1

Which will give the output

[1]+  Exit 2                  ./rubbish-script.sh

The process is now dead. I have never encountered a situation when this trick has failed to kill a process that could not otherwise be killed.