In this tutorial we will explain how to manage processes on CentOS 6.4, Debian or Ubuntu platform.

Requirements

  • CentOS 6.4, Debian or Ubuntu installed on your computer/server
  • SSH access (Command line access to the server)
  • root privileges
  • Basic skills for working on a Linux environment

Every program that runs on the server can generate one or more processes. On one server there might be multiple users executing multiple commands which will generate processes. They can run in the foreground, occupying the terminal that started the program, where you can't start other applications as long as this process is running in the foreground. Alternatively, they can run in the background, so that the terminal in which you started the program can accept new commands while the program is running. There is another type of processes called daemons. They are server processes that run continuously and listen for requests on certain ports. Most of the time, they are initialized at system startup and then wait in the background until their service is required.

Using ps

Using the Process Status command we can list all running processes:

The above command displays all processes owned by the current user.

There are few options which can be used with the ps command for more detailed information and better layout:

Here is a short explanation of every column:

USER – the owner of the process
PID – a unique process identification number
%CPU – CPU utilization of the process
%MEM - ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage
VSZ - virtual memory size of the process in KiB
RSS - the non-swapped physical memory that a task has used in KiB
TTY – controlling terminal
STAT - multi-character process state. Here is a list of the process states:
D - Uninterruptible sleep (usually IO)
R - Running or runnable (on run queue)
S - Interruptible sleep (waiting for an event to complete)
T - Stopped, either by a job control signal or because it is being traced.
W - paging (not valid since the 2.6.xx kernel)
X - dead (should never be seen)
Z - Defunct ("zombie") process, terminated but not reaped by its parent.

For BSD formats and when the stat keyword is used, additional characters may be displayed:

< - high-priority (not nice to other users) N - low-priority (nice to other users) L - has pages locked into memory (for real-time and custom IO) s - is a session leader l - is multi-threaded (using CLONE_THREAD, like NPTL pthreads do) + - is in the foreground process group START - starting time or date of the process
TIME - cumulative CPU time, "[dd-]hh:mm:ss" format.
COMMAND – the executed command with all its arguments as a string

The relations between processes can be visualized with the following options:

Alternative way to check the process relation is using the pstree command:

Another useful command which provides information about the running processes, CPU/memory utilization and system uptime is:

The first section of the output provides system statistics related to server load, total number of processes and CPU and memory utilization.

Here is a short explanation of each column:

PID – the process ID
USER – the process owner
PR – the process priority – can vary between -20 (highest) and 19 (lowest)
NI – the process nice value
VIRT - Virtual memory used (kb)
RES - Physical memory used (kb)
SHR - Physical memory used (kb)
S – process state:
          'D' = uninterruptible sleep
          'R' = running
          'S' = sleeping
          'T' = traced or stopped
          'Z' = zombie

%CPU – percent of CPU time
%MRM - Physical memory used
TIME+ - total CPU time
COMMAND – the executed command

Using kill

The Linux processes can be managed with signals. The kill command can be used to send signal to a process where the default signal is SIGTERM which terminates the process:

where PID is the process ID.

The above command will terminate the process with the corresponding ID.

The kill signals not only stop/kill processes but they also pause, continue and restart processes. A full signals list can be displayed with the following command:

Each signal can be sent either with its name or number, for example in order to kill a process we can use:

If we want to send signal to a process owned by another user we should use the sudo command:

Alternative command that can be used with multiple processes is killall:

It sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent.

Using nice

The Linux processes can be prioritized with a value called niceness. Processes with higher priority are less nice because they might require larger amount of system resources while low priority processes are nicer. Nicenesses range from -20 (most favorable scheduling) to 19 (least favorable). Niceness value of 0 is the default priority.

In order to execute a new command with desired niceness value we can use:

If the command was already executed we should use the renice command with the corresponding process ID in order to change its niceness:

Output similar to the following one should be displayed upon execution: