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
top and then issue the
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 from the terminal it using:
This will suspend the process and give you back control of the terminal. Running this command will produce the following output:
+ 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 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:
Which will give the output
+ 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.