Sending Signals to Processes: kill, killall and top

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, you enter the command:

     $ kill 785
    

If you want to send it signal 19 (STOP), you 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:

$ killall -9 mozilla

Whatever happens, you will only kill your own processes (unless you are root) so you do not need worry about your “neighbor's” processes if you're running on a multi-users system if they have the same name since they will not be affected.

Mixing ps and kill: top

top is a program that 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 6.1.

Figure 6.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 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 sort 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 states R, Running) and not the others. Using this command again takes you back to showing all processes.