Each running tasks or programs in a system is called a process in the operating system terminology. At certain times, it is possible for a program to get into a state where it is no longer responsive and hence be need to be terminated manually.
Programs and processes can be launched in multiple different ways. The following steps will attempt to terminate a process that was launched from a command shell. The methods to terminate a process that is not attached to a shell or terminal is dealt with later in the post.
If the program was started from a command shell and the process is still attached to the shell, then you might be able to terminate the process by doing a control-c on the command shell.
The Control-C sends a SIGINT or an interrupt signal to the currently active process. Most programs will shutdown but it is also possible for the program to ignore this signal depending on its current state.
If a program is hanging or is in a non-responsive state then Control-C might not be effective in terminating the process.
When Control-c does not work, try issuing Control-\. This sends a SIGQUIT or the quit signal to the active process. As in the case of the previous SIGINT signal, it is possible again for the process to ignore this signal.
If again this does not work, then your best bet is to use the kill command against the process. In order to do that, you need to put the offending process that is currently active into a sleep or suspended mode.
Control-z and kill
Issuing a Control-z on the shell will suspend the currently running process and return you a prompt. Also, it will print out the job number of the process. You can issue a kill command against this job number in order to terminate the process. If the job number is 2, then
bash$ kill -9 %2
where 2 is the job number of the process that is suspended.
If you are unsure of the job number, then you can see all the running jobs in the shell by using the jobs command. The number in the square brackets is the job number.
- Running gedit &
Background and kill
If the above kill of the suspended process does not terminate the process, then you might have to put the process in the background and try kill again. Sometimes a suspended process might be holding on or waiting for device or resource which might prohibit it from terminating.
In order to put the suspended process into an active state, you can issue the bg command. This will start running the last suspended process (using Control-z) as a background process.
Once this is an active process, you can issue a kill command against the process …
bash$ kill -9 $!
The $! is a special environment variable that holds the process id (pid) of the last background-ed process in the shell. You can also use the exact process id of the process.
In some cases, it is quite possible that the process resists being put into the background or that the shell itself is frozen for some reason. In such cases, you will need another shell as well as the process id of the process in order to terminate the process.
This is also the case when the process was not launched from a command shell and hence is not attached to a command shell.
Kill with Process id
If you know the process id or pid of the process, then issue a kill command against the process.
bash$ kill -9 9876
where 9876 is the process id. The -9 option is a special kill signal, SIGKILL that is more powerful than SIGINT or SIGQUIT.
The important aspect of the above command is that you can find the process id of the process correctly, using the information you have and know.
how to find the pid or process id
The commands that you can use to find the process id from the process name, is pidof, pgrep and ps. To find the process id of a process named kate, you can do
bash$ pidof kate
the output 9876 is the process id. You can also use the ps command and grep for the process name, as
bash$ ps aux | grep kate
killall is a Linux specific command that can be used to kill all processes by name. If you have one or more processes by the same name, then killall will terminate all those processes.
bash$ killall -9 kate
killall has some additional options to kill a process based on age and user names.
pkill is yet an other command line utility that can be used to send signals to processes. Though originally available only on Solaris, it has since been implemented in some linux distros. pkill differs from the original kill commands in that it has some support for regular expressions.
bash$ pkill kate
For all practical purposes, killall and pkill commands works almost in a similar manner. Though, I have noticed that pkill can be a little more aggressive in matching process names and will kill processes with even partially matching names.