Managing processes in Linux

Thursday, 8 November 2007, adz

As we promised, we continue our “Console tricks” saga covering a bit more advanced topics now. Today we’re going to learn the idea of process in Linux and we’ll excel at managing, prioritizing and… killing them(!) Have a good reading.

1. Basics

Before we begin, it would be reasonable to give a process definition and explain the meaning of a couple of the connected terms. Generally speaking, a process is a program being executed that exists in some particular state. These states are:

  • new - the process was just created
  • ready - the process is waiting to be executed by the system’s CPU
  • running - the process is currently executed by the system’s CPU
  • blocked - the process is waiting for an event or for a resource to be assigned
  • ended - the process is finishing its action

The process receives access to the system resources such as CPU, storage, files, and I/O devices. Each process has its own Process ID (PID) and Parent Process ID (PPID). Where one may present running processes in a tree-form, a process with PID=0 (the init process) is a root. As in files, each case process has an owner assigned. The owner of the process is a user who has executed a given program. There is one exception:when the program runs with setuid or setgid bit. The process’ access rights depend on a users access rights to the system’s resources (e.g., files in the /dev directory). To communicate with the processes, so called signals are initiated and used. These programs are executed with different priorities, defined by a number nice. The number gets values from -20 (the highest) to 19 (the lowest), the default value being 0.

2. fg, bg, jobs

A terminal enables us to run a couple of programs in the same time. Switching between one terminal to another is done simply by moving one program from “the first plan” to the “background”.: only first-plan-process can receive data from the user. One can compare moving processes to the active window, where it covers other (inactive) windows.

To run a program in the background, it is enough to add an $ (Ampersand) sign at the end of a command.

$ mpg123 -q file.mp3&
[1] 638
$

Running mpg123 in the background, I can listen to the music without loosing my access to shell. The shell displayed the job number in the square brackets -1 and its PID - 638.

To display a list of background jobs one should use the jobs command. It has an additional parameter, -l, that additionally gives the PID number of a given job.

To stop first-plan-process, one should use Ctrl + z (in this case the process receives signal SIGSTP)and Ctrl + c (the process receives signal SIGINT) to stop a process.

$ jobs -l
[1]-   638 Running                 mpg123 -q file.mp3 &
[2]+   878 Stopped (tty output)    mc

This example shows that two jobs are running in the background: mpg123 is running since it does not need any interaction with user, and mc is being stopped and waiting for data.

To make a specific process run, the first-plan-process one should use the fg %job number command, or fg %?command name, or just a part of the name.

$ fg %1
$ fg %mpg

In the first example, I’ve moved the job to the first-plan using its job number. In the second example, I’ve used a part of the name.

The fg %job number command will run stopped background-process, and — not like fg — will not move this process to the first plan.

$ mpg123 -q file.mp3
Ctrl + z
[3]+  Stopped                 mpg123 -q file.mp3
$ bg %3
[3]+ mpg123 -q file.mp3 &

It is worth noticing one more thing here: data, sent by the background-program, are displayed on the screen (the standard output). To change this you may use a pipe to redirect the stream to trash, e.g. command > /dev/null

3. ps, pstree

The command jobs presented earlier in this article display processes that are currently executed on a given terminal. To view the full list of processes executed within a system one may use the ps command. This command, used without options, works just like jobs. Full descriptions of all the options used by the ps command are available after typing man ps. Here are the most interesting options:

  • -e displays all the processes that are executed by the system
  • -l provides a detailed description
  • -f provides a detailed description, but shorted than when -l is used
  • -H displays all the processes in a ps tree-like form
  • a displays all the processes
  • x displays all the processes that were run without terminal usage
  • u processes the user name
  • f displays the processes in tree-like form

We can add options to ps in two formats: either the System V format (options with -) or the BSD format. The output of the command with options aux appear similar to this:

$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1   2952  1852 ?        Ss   10:49   0:01 /sbin/init
adam      5623  0.0  0.6  27892  7100 ?        Ssl  10:51   0:00 /usr/bin/gnome-session
adam      6815  0.2  1.6  39880 16728 ?        S    11:11   0:14 gedit
adam      7105  0.0  0.2   5708  3060 pts/1    Ss   11:16   0:00 bash

The meaning of the column headings:

  • USER - user name, the process owner
  • PID - the process id
  • %CPU - estimated percent of CPU usage (it is the time of CPU usage by the process divided by the system uptime)
  • %MEM - estimated memory usage
  • VSZ - virtual memory usage in KB
  • RSS - physical memory usage in KB
  • TTY - terminal on which the process were executed
  • STAT - the process state, D = I\O data awaiting, R = is executed in the moment, S = sleep, T = stopped, X dead (it should not been displayed), Z = zombie process
  • START = - hour when the process started to run
  • TIME - the time of CPU usage
  • COMMAND - the command that started the process

The pstree command displays processes in a tree-like form. We can add the option -a to display the command that started the process.

$ pstree -a
init
  ├─cupsd
  ├─dbus-daemon –system
  ├─gedit
  └─gnome-terminal
      ├─bash
      │   └─man ps
      │       └─pager -s
      ├─bash
      │   └─man ps
      │       └─pager -s
      └─bash
          └─pstree -a

3. top

The top command enables the user to see the processes in real time. The option -u user name enables displaying only selected users’ processes, -pPID enables displaying the process with a selected PID (this option can be added up to 20 times).

$ top
top - 17:38:09 up 43 min,  2 users,  load average: 0.32, 0.17, 0.06
Tasks: 124 total,   1 running, 123 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.5%us,  0.2%sy,  0.0%ni, 98.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1026304k total,   560996k used,   465308k free,    42120k buffers
Swap:  1020116k total,        0k used,  1020116k free,   246652k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 7391 adam      15   0  189m  56m  21m S    3  5.6   0:20.42 firefox-bin
 7517 adam      15   0  2368 1164  876 R    1  0.1   0:00.03 top
 4029 root      13  -2  1744  384  280 S    0  0.0   0:00.76 ipw3945d-2

To quit this program one should use q. Useful keybinds:

  • F or O to change sorting
  • space to refresh the screen
  • < , > for sorting using displayed columns
  • r to change the nice value of the process
  • W to save current top settings to file ~/.toprc
  • k to send a signal to the process

4. htop

htop displays the processes list in a more sophisticated graphical form. This program uses the ncurses library. This library is usually it is not available by default.

  • cursors - moving between processes
  • F1 - help menu
  • F2 - settings
  • F3 - find a process
  • F4 - switch sorting
  • F5 - display processes in tree-like form
  • F6 - change sorting type
  • F7 - decrease the priority of selected process
  • F8 - increase the priority of selected process
  • F9 - send a signal to a process
  • F10 - quit

5. kill

It sends signals to the processes. Syntax is following: kill signal PID. The default signal is SIGTERM (value: 15). It “gently” ends the program, allowing it to close open files and “clean up” the working environment and any consequences. Stronger is another SIGKILL (value 9). It enforces a program to stop immediately. The SIGSTOP signal stops the SIGKILL execution and the SIGCONT signal brings the execution back online. You can send a signal to a process using its name or its numerical value.

$ kill -SIGKILL 7496
kill -9 7496

In both cases the process with PID 7496 will be “killed”.

6. killall

Using this command you may use the process’ name (regular expressions might also be used) e.g.:

$ killall mplayer

It kills all the processes named mplayer. An interesting option is -l. This option displays all the possible signals. In some systems, like Solaris, this command kills all the processes that the user can kill. If it is used by root, it shutdowns the system.

7. nohup

This command is connected with a SIGHUP signal. Historically, this signal reported on terminal connection loss. Today, it is used to inform that the pseudo-terminal on which the program is running has stopped working (or has finished), and all the programs have ended their work as well (or have also finished). The process started with the nohup command will ignore this signal.

$ nohup pidgin
nohup: add results to `nohup.out'

If the program’s output was not redirected to a file, nohup redirects it to the nohup.out by default.

8. pidof, pgrep

This are small but useful commands, that gives PID of the command.

$ pidof nautilus
5668
$ pgrep nautilus
5668

Pgrep can be used with option -l, that displays the program’s name and PID. We can use also -u users’ name, -G group name that displays processes belonging to the selected user or group.

$ kill `pidof mplayer`

In this example, I used pidof to close mplayer without knowing its PID.

9. renice

This command changes the priority of the process. The highest possible priority is -20 and the lowest is 19. These priority changes can be performed only by root.

$ renice 19 `pidof top`
12344: old priority 0, new priority 19

Option -u users’ name changes priority of all processes of that user, where -g group name changes priority of all processes of that group.

$ renice 5 -u adam
1000: old priority 2, new priority 5

This article is part of the Command line tricks series, season II. Read the previous article of the series: Getting help in Linux » and the next one: Permissions in Linux »

Translated by Paweł Łupkowski, Proof-read by fgibbs

Subscribe to RSS feed for this article!

Comments

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI

Adjust field size: shrink | enlarge)

You can use simple HTML in your comments. Some examples are as follows:
  • A hyperlink: <a href="polishlinux.org">GNU/Linux for everyone!</a>,
  • Strong text: <strong>Strong text</strong>,
  • Italic text: <em>italic text</em>,
  • Strike: <strike>strike</strike>,
  • Code: <code>printf("hello world");</code>,
  • Block quote: <blockquote>Block quote</blockquote>