3. Sending Signals to Processes: kill, killall and top

3.1. kill, killall

These two commands are used to send signals to processes. The kill command requires a process number as an argument, while killall requires a process name.

Both of these commands can optionally receive the signal number of the signal to be sent as an argument. By default, they both send the signal 15 (TERM) to the relevant process(es). For example, if you want to kill the process with PID 785, enter the command:

$ kill 785

If you want to send it signal 19 (STOP), enter:

$ kill -19 785

Suppose instead that you want to kill a process where you know the command name. Instead of finding the process number using ps, you can kill the process by its name. If the process name is “mozilla” you could issue the command:

$ killall -9 mozilla

Whatever happens, you will only kill your own processes (unless you are root) so you do not need to worry about other users' processes if you are running on a multi-user system, since they will not be affected.

3.2. Mixing ps and kill: top

top is a program which simultaneously fulfills the functions of ps and kill, and is also used to monitor processes in real-time giving information about CPU and memory usage, running time, etc., as shown in Figure 10.1, “Monitoring Processes with top”.

Figure 10.1. Monitoring Processes with top

Monitoring Processes with top

The top utility is entirely keyboard controlled. You can access help by pressing h. Its most useful commands are the following:

  • k: this command is used to send a signal to a process. top will then ask you for the process' PID followed by the number or the name of the signal to be sent (TERM or 15 by default);

  • M: this command is used to sort processes by the amount of memory they take up (field %MEM);

  • P: this command is used to sort processes by the CPU time they take up (field %CPU): this is the default sorting method;

  • u: this one is used to display a given user's processes. top will ask you which one. You need to enter the user's name, not his UID. If you do not enter any name, all processes will be displayed;

  • i: by default, all processes, even sleeping ones, are displayed. This command ensures that only processes currently running are displayed (processes whose STAT field shows R, Running) and not the others. Using this command again takes you back to showing all processes.

  • r: this command is used to change the priority of a selected process.