From the system's point of view, applications run in one or many processes, consuming system resources, such as memory and processor time. We present some commands to monitor and manage those processes, and as a consequence, the applications they belong to.
The ps command displays a list of processes currently running on the system, according to the criteria you set.
Running ps without arguments will show only processes initiated by you and attached to the terminal you are using:
$ ps PID TTY TIME CMD 18614 pts/3 00:00:00 bash 20173 pts/3 00:00:00 ps
Processes are controlled by means of signals. The kill and killall commands are used to send signals to processes. The kill command requires a process number as an argument, while killall requires a process name.
Note | |
---|---|
Please bear in mind that processes react differently to the same signals. You cannot expect a priori that the process behaves as you thought it would when you send it a signal. |
kill [-SIGNAL] <process_number>
killall [-SIGNAL] <process_name>
Signals can be specified by number or name. Execute kill -l to view a list of available signals. The most commonly used signals are:
TERM
or 15
: this is
the default signal sent if the signal name or number is
omitted. It terminates the process gracefully.
STOP
or 19
: this
signal is used to temporarily pause a process. Send signal
CONT
or 18
to continue a
paused process.
KILL
or 9
: this
signal is used to force process termination. It is commonly used
to end a process that is not responding anymore (or
“frozen”). It terminates the process
abruptly.
kill 785: asks the process identified by
the number 785
to finish execution giving the
process the chance to perform any clean-up operation it
needs;
kill -KILL 785: forces termination of the
process identified by the number 785
without
giving the process the chance to perform any clean-up
operation. The process ends immediately;
killall -TERM make: asks all processes
named make
launched by this user to finish
execution.
Whatever
happens, you will only control your own processes
(unless you are root
) so you do not need to worry about
other users' processes since they will not be affected.
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 17.1, “Monitoring Processes with top”.
The top utility is entirely keyboard controlled. Commands are case-sensitive. You can access help by pressing h. Its most useful commands are the following:
k: sends a signal to a
process. You are asked for the process' PID followed by the
number or the name of the signal to be sent
(TERM
or 15
, by
default);
M: sorts display of processes by
the amount of memory they take up (field
%MEM
);
P: sorts display of processes by
the CPU time they take up (field %CPU
):
this is the default sorting method;
u: displays a given user's processes. You are asked to enter the user's name, not his UID. If you do not enter any name, all processes are 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.