Introduction to Process Management

For everything that happens on a Linux server, a process is started. For that reason, process management is among the key skills that an administrator has to master. To do this efficiently, it is important to know which type of process you are dealing with. A major distinction can be made between two process types:

When a process is started, it can use multiple threads. A thread is a task started by a process and that a dedicated CPU can service. The Linux shell does not offer tools to manage individual threads. Thread management should be taken care of from within the command.

To manage a process efficiently, it is paramount that you know what type of process you are dealing with. Shell jobs require another approach than the processes that are automatically started when a computer boots.

Managing Shell Jobs

When a user types a command and press Enter, a shell job is started. If no particular measures have been taken, the job is started as a foreground process, occupying the terminal it was started from until it has finished its work. As a Linux administrator, you need to know how to start shell jobs in the foreground or background and what can be done to manage shell jobs.

When a process is started from a shell, it becomes a child process of that shell. In process management, the parent-child relationship between processes is very important. The parent is needed to manage the child. For that reason, all processes started from a shell are terminated when that shell is stopped. This also offers an easy way to terminate processes no longer needed.

Processes started in the background will not be killed when the parent shell from which they were started is killed. To terminate these processes, you need to use the kill  command, as described later in this chapter.

Note

In earlier versions of the bash shell, background processes were also killed when the shell they were started from was terminated. To prevent that, the process could be started with the nohup  command in front of it. Using nohup  for this purpose is no longer needed in RHEL 7.

Running Jobs in the Foreground and Background

By default, any executed command is started as a foreground job. For many commands, that does not really matter because the command often takes a little while to complete, after which it returns access to the shell from which it was started. Sometimes it might prove useful to start commands in the background. This makes sense for processes that do not require user interaction. A process that does require user interaction will not be able to get that when running in the background, and for that reason will typically stall when moved to the background. You can take two different approaches to run a process in the background.

If you know that a job will take a long time to complete, you can start it with an &  behind it. This immediately starts the job in the background to make room for other tasks to be started from the command line. To move the last job that was started in the background back as a foreground job, use the fg  command. This command immediately, and with no further questions, brings the last job back to the foreground.

A job might sometimes have been started that takes (much) longer than predicted. If that happens, you can use Ctrl+Z  to temporarily stop the job. This does not remove the job from memory; it just pauses the job so that it can be managed. Once paused, it can be continued as a background job using the bg  command. An alternative key sequence that you can use to manage shell jobs is Ctrl+C. This stops the current job and removes it from memory.

A related keystroke combination is Ctrl+D, which sends the End Of File (EOF) character to the current job. The result is that the job stops waiting for further input so that it can complete what it was currently doing. The result of sending Ctrl+D  is sometimes very similar to the result of sending Ctrl+C, but there is a difference. When Ctrl+C  is used, the job is just canceled, and nothing is closed properly. When Ctrl+D  is used, the job stops waiting for further input, which often is just what is needed to complete in a proper way.

Moving jobs between the foreground and background

When moving jobs between the foreground and background, it may be useful to have an overview of all current jobs. To get such an overview, use the jobs  command. Job numbers can be used as an argument to the fg  and bg  commands to perform job management tasks.

Exercise ( modified from Sander van Vugt book) 

You read how to manage interactive shell jobs in this section. Notice that all of these jobs are processes as well. As the user who started the job, you can also manage it. You need to be root to manage process of other users, or be able to switch you identituy to the identity of the other user via sudo.

  1.   Open a root shell and type the following commands:
    sleep 3600 &
    dd if=/dev/zero of=/dev/null &
    sleep 7200
  2.   Because you started the last command with no &  after the command, you have to wait 2 hours before you get control to the shell back. Type Ctrl+Z  to stop it.

  3.   Type jobs. You will see the three jobs that you just started. The first two of them have the Running state, and the last job currently is in the Stopped state.

  4.   Type bg 3  to continue running job 3 in the background. Notice that because it was started as the last job, you did not really have to add the number 3.

  5.   Type fg 2  to move job 1 to the foreground.

  6.   Press Ctrl+C  to cancel job number 2 and use jobs  to confirm that it is now gone.

  7. Type jobs  again to see remaining jobs.

NOTES:

Exercise  2: behaviour of background jobs in case session is terminated.

When you use & you disconnect the job from the terminal. So ending terminal session in RHEL 7 does not kill this background job.  In earlier versions of RHEL background processes were all killed when the shell they were started from was terminated. To prevent that, the process could be started with the nohup  command in front of it. Using nohup  for this purpose is no longer needed in RHEL 7.

Let's check this in this exercise.

  1. Type dd if=/dev/zero of=/dev/null &.

  2. Open the second terminal and type top. You will see dd job among the top jobs running (this job simply waste a lot of CPU time)
  3. Now exit from the first terminal. You will see on the second terminal that the job is still running and is present in top listing -- exiting terminal session does not kill background jobs, which are already disconnected from the terminal.

  4. From the top screen use k  to kill the dd job.

    Use Ctrl-C to exit top

 

Using Common Command-Line Tools for Process Management

On a Linux server, many processes are usually running. On an average server or desktop computer, there are often more than a hundred active processes. With so many processes being active, things may go wrong. If that happens, it is good to know how noninteractive processes can be stopped, or how the priority of these processes can be adjusted to make more system resources available for other processes.

Tasks on Linux are typically started as processes. One process can start several worker threads. Working with threads makes sense, because if the process is very busy, the threads can be handled by different CPUs or CPU cores available in the machine. As a Linux administrator, you cannot manage individual threads; you can manage processes, though. It is the programmer of the multithreaded application that has to define how threads relate to one another.

Before talking about different ways to manage processes, it is good to know that there are two different types of background processes. To start, there are kernel threads. These are a part of the Linux kernel, and each of them is started with its own process identification number (PID). When managing processes, it is easy to recognize the kernel processes because they have a name that is between square brackets. For example here is the result off  execution of the command  ps aux | head   which list forst dozen of kernel threads

[0]d620@ROOT:~ # ps aux | head
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 128160  6716 ?        Ss   Mar01   0:18 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2  0.0  0.0      0     0 ?        S    Mar01   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Mar01   0:08 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   Mar01   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    Mar01   0:00 [migration/0]
root         8  0.0  0.0      0     0 ?        S    Mar01   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        S    Mar01   0:14 [rcu_sched]
root        10  0.0  0.0      0     0 ?        S<   Mar01   0:00 [lru-add-drain]
root        11  0.0  0.0      0     0 ?        S    Mar01   0:04 [watchdog/0]

As you see in RHEL7 the process with  PID 1 is systemd. Kernel threads cannot be managed. 


Using ps to Get Process Information

The most common command to get an overview of currently running processes is ps. If used without any arguments, the ps  command shows only those processes that have been started by the current user. You can use many different options to display different process properties. If you are looking for a short summary of the active processes, use ps aux  (as you saw in Listing 9.1). If you are not only looking for the name of the process but also for the exact command that was used to start the process, use ps -ef. Alternative ways to use ps  exist as well, such as the command ps fax, which shows hierarchical relationships between parent and child processes

Use ps -ef  to See the Exact Command Used to Start Processes

Click here to view code image


[0]d620@ROOT:~ # ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jan17 ?        00:00:43 /usr/lib/systemd/systemd --switched-root --sy
root         2     0  0 Jan17 ?        00:00:00 [kthreadd]
root         3     2  0 Jan17 ?        00:00:16 [ksoftirqd/0]
root         5     2  0 Jan17 ?        00:00:00 [kworker/0:0H]
root         7     2  0 Jan17 ?        00:00:00 [migration/0]
root         8     2  0 Jan17 ?        00:00:00 [rcu_bh]
root         9     2  0 Jan17 ?        00:00:38 [rcu_sched]
root        10     2  0 Jan17 ?        00:00:00 [lru-add-drain]
root        11     2  0 Jan17 ?        00:00:12 [watchdog/0]
root        12     2  0 Jan17 ?        00:00:11 [watchdog/1]
root        13     2  0 Jan17 ?        00:00:00 [migration/1]
root        14     2  0 Jan17 ?        00:00:01 [ksoftirqd/1]
root        16     2  0 Jan17 ?        00:00:00 [kworker/1:0H]
root        19     2  0 Jan17 ?        00:00:00 [kdevtmpfs]
root        20     2  0 Jan17 ?        00:00:00 [netns]
root        21     2  0 Jan17 ?        00:00:01 [khungtaskd]
root        22     2  0 Jan17 ?        00:00:00 [writeback]
root        23     2  0 Jan17 ?        00:00:00 [kintegrityd]
root        24     2  0 Jan17 ?        00:00:00 [bioset]
root        25     2  0 Jan17 ?        00:00:00 [bioset]
root        26     2  0 Jan17 ?        00:00:00 [bioset]
root        27     2  0 Jan17 ?        00:00:00 [kblockd]
root        28     2  0 Jan17 ?        00:00:00 [md]
root        29     2  0 Jan17 ?        00:00:00 [edac-poller]
root        30     2  0 Jan17 ?        00:00:00 [watchdogd]
root        36     2  0 Jan17 ?        00:00:02 [kswapd0]
root        37     2  0 Jan17 ?        00:00:00 [ksmd]
root        38     2  0 Jan17 ?        00:00:10 [khugepaged]
root        39     2  0 Jan17 ?        00:00:00 [crypto]
root        47     2  0 Jan17 ?        00:00:00 [kthrotld]
root        50     2  0 Jan17 ?        00:00:00 [kmpath_rdacd]
root        51     2  0 Jan17 ?        00:00:00 [kaluad]
root        52     2  0 Jan17 ?        00:00:00 [kpsmoused]
root        54     2  0 Jan17 ?        00:00:00 [ipv6_addrconf]
root        67     2  0 Jan17 ?        00:00:00 [deferwq]
root        99     2  0 Jan17 ?        00:00:00 [kauditd]
root       939     2  0 Jan17 ?        00:00:00 [ata_sff]
root       958     2  0 Jan17 ?        00:00:00 [scsi_eh_0]
root       964     2  0 Jan17 ?        00:00:00 [scsi_tmf_0]
root       965     2  0 Jan17 ?        00:00:00 [scsi_eh_1]
root       974     2  0 Jan17 ?        00:00:00 [scsi_tmf_1]
root       976     2  0 Jan17 ?        00:00:00 [pccardd]
root      1491     2  0 Jan17 ?        00:00:00 [bioset]
root      1493     2  0 Jan17 ?        00:00:00 [xfsalloc]
root      1500     2  0 Jan17 ?        00:00:00 [xfs_mru_cache]
root      1507     2  0 Jan17 ?        00:00:00 [xfs-buf/sda1]
root      1508     2  0 Jan17 ?        00:00:00 [xfs-data/sda1]
root      1512     2  0 Jan17 ?        00:00:00 [xfs-conv/sda1]
root      1513     2  0 Jan17 ?        00:00:00 [xfs-cil/sda1]
root      1514     2  0 Jan17 ?        00:00:00 [xfs-reclaim/sda]
root      1515     2  0 Jan17 ?        00:00:00 [xfs-log/sda1]
root      1516     2  0 Jan17 ?        00:00:00 [xfs-eofblocks/s]
root      1517     2  0 Jan17 ?        00:00:51 [xfsaild/sda1]
root      1518     2  0 Jan17 ?        00:00:07 [kworker/0:1H]
root      1587     1  0 Jan17 ?        00:00:11 /usr/lib/systemd/systemd-journald
root      1603     2  0 Jan17 ?        00:00:04 [kworker/1:1H]
root      1608     1  0 Jan17 ?        00:00:00 /usr/sbin/lvmetad -f
root      1620     1  0 Jan17 ?        00:00:01 /usr/lib/systemd/systemd-udevd
root      2429     2  0 Jan17 ?        00:00:00 [cfg80211]
root      2640     2  0 Jan17 ?        00:00:00 [iwl3945]
root      2703     2  0 Jan17 ?        00:00:00 [kvm-irqfd-clean]
root      3135     2  0 Jan17 ?        00:00:00 [xfs-buf/sda2]
root      3136     2  0 Jan17 ?        00:00:00 [xfs-data/sda2]
root      3137     2  0 Jan17 ?        00:00:00 [xfs-conv/sda2]
root      3138     2  0 Jan17 ?        00:00:00 [xfs-cil/sda2]
root      3139     2  0 Jan17 ?        00:00:00 [xfs-reclaim/sda]
root      3140     2  0 Jan17 ?        00:00:00 [xfs-log/sda2]
root      3141     2  0 Jan17 ?        00:00:00 [xfs-eofblocks/s]
root      3142     2  0 Jan17 ?        00:00:02 [xfsaild/sda2]
root      3163     1  0 Jan17 ?        00:00:02 /sbin/auditd
root      3187     1  0 Jan17 ?        00:00:10 /usr/lib/systemd/systemd-logind
dbus      3188     1  0 Jan17 ?        00:00:14 /usr/bin/dbus-daemon --system --address=syste
polkitd   3208     1  0 Jan17 ?        00:00:02 /usr/lib/polkit-1/polkitd --no-debug
root      3209     1  0 Jan17 ?        00:06:41 /usr/sbin/irqbalance --foreground
root      3252     1  0 Jan17 ?        00:00:02 /usr/bin/python -Es /usr/sbin/firewalld --nof
root      3257     1  0 Jan17 ?        00:00:05 /usr/sbin/crond -n
chrony    3268     1  0 Jan17 ?        00:00:04 /usr/sbin/chronyd
root      3273     1  0 Jan17 ?        00:00:00 login -- root
root      3302     1  0 Jan17 ?        00:00:49 /usr/sbin/NetworkManager --no-daemon
root      3607     1  0 Jan17 ?        00:00:13 /usr/sbin/wpa_supplicant -u -f /var/log/wpa_s
root      3813     1  0 Jan17 ?        00:05:49 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      3815     1  0 Jan17 ?        01:19:38 /usr/bin/dockerd-current --add-runtime docker
root      3816     1  0 Jan17 ?        00:02:26 /usr/sbin/rsyslogd -n
root      4217     1  0 Jan17 ?        00:00:15 /usr/libexec/postfix/master -w
postfix   4219  4217  0 Jan17 ?        00:00:02 qmgr -l -t unix -u
root      4224  3815  0 Jan17 ?        00:50:51 /usr/bin/docker-containerd-current -l unix://
root      4785     2  0 Feb09 ?        00:00:01 [kworker/u4:18]
root      4843     2  0 Feb12 ?        00:00:00 [kworker/u4:76]
root      4941  3302  0 Feb12 ?        00:00:00 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp
root      4988  3273  0 Feb12 tty1     00:00:00 -bash
root      5576     2  0 01:30 ?        00:00:00 [kworker/1:2]
root      6274     2  0 06:01 ?        00:00:08 [kworker/0:3]
root      6337     2  0 09:00 ?        00:00:02 [kworker/1:0]
root      6411     2  0 12:00 ?        00:00:02 [kworker/0:0]
postfix   6450  4217  0 13:03 ?        00:00:00 pickup -l -t unix -u
root      6474     2  0 14:01 ?        00:00:00 [kworker/0:1]
root      6501 10647  0 14:11 ?        00:00:00 sshd: root@pts/0
root      6504  6501  0 14:11 pts/0    00:00:00 -bash
root      6525     2  0 14:11 ?        00:00:00 [kworker/0:2]
root      6531  6504  0 14:13 pts/0    00:00:00 ps -ef
root     10647     1  0 Jan27 ?        00:00:00 /usr/sbin/sshd -D
root     23452     2  0 Jan27 ?        00:00:00 [scsi_eh_2]
root     23453     2  0 Jan27 ?        00:00:00 [scsi_tmf_2]
root     23454     2  0 Jan27 ?        00:00:44 [usb-storage]

Note

For some commands, using a hyphen before options is optional. Some commands do not. The ps  command is one of those latter commands. There is a historical reason why this is the case: The commands derive from the old BSD UNIX flavor, where it was common to specify command-line options without a - in front of them.


Use ps fax  to Show Parent-Child Relationships Between Processes

[0]d620@ROOT:~ # ps fax
  PID TTY      STAT   TIME COMMAND
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:16  \_ [ksoftirqd/0]
    5 ?        S<     0:00  \_ [kworker/0:0H]
    7 ?        S      0:00  \_ [migration/0]
    8 ?        S      0:00  \_ [rcu_bh]
    9 ?        S      0:38  \_ [rcu_sched]
   10 ?        S<     0:00  \_ [lru-add-drain]
   11 ?        S      0:12  \_ [watchdog/0]
   12 ?        S      0:11  \_ [watchdog/1]
   13 ?        S      0:00  \_ [migration/1]
   14 ?        S      0:01  \_ [ksoftirqd/1]
   16 ?        S<     0:00  \_ [kworker/1:0H]
   19 ?        S      0:00  \_ [kdevtmpfs]
   20 ?        S<     0:00  \_ [netns]
   21 ?        S      0:01  \_ [khungtaskd]
   22 ?        S<     0:00  \_ [writeback]
   23 ?        S<     0:00  \_ [kintegrityd]
   24 ?        S<     0:00  \_ [bioset]
   25 ?        S<     0:00  \_ [bioset]
   26 ?        S<     0:00  \_ [bioset]
   27 ?        S<     0:00  \_ [kblockd]
   28 ?        S<     0:00  \_ [md]
   29 ?        S<     0:00  \_ [edac-poller]
   30 ?        S<     0:00  \_ [watchdogd]
   36 ?        S      0:02  \_ [kswapd0]
   37 ?        SN     0:00  \_ [ksmd]
   38 ?        SN     0:10  \_ [khugepaged]
   39 ?        S<     0:00  \_ [crypto]
   47 ?        S<     0:00  \_ [kthrotld]
   50 ?        S<     0:00  \_ [kmpath_rdacd]
   51 ?        S<     0:00  \_ [kaluad]
   52 ?        S<     0:00  \_ [kpsmoused]
   54 ?        S<     0:00  \_ [ipv6_addrconf]
   67 ?        S<     0:00  \_ [deferwq]
   99 ?        S      0:00  \_ [kauditd]
  939 ?        S<     0:00  \_ [ata_sff]
  958 ?        S      0:00  \_ [scsi_eh_0]
  964 ?        S<     0:00  \_ [scsi_tmf_0]
  965 ?        S      0:00  \_ [scsi_eh_1]
  974 ?        S<     0:00  \_ [scsi_tmf_1]
  976 ?        S      0:00  \_ [pccardd]
 1491 ?        S<     0:00  \_ [bioset]
 1493 ?        S<     0:00  \_ [xfsalloc]
 1500 ?        S<     0:00  \_ [xfs_mru_cache]
 1507 ?        S<     0:00  \_ [xfs-buf/sda1]
 1508 ?        S<     0:00  \_ [xfs-data/sda1]
 1512 ?        S<     0:00  \_ [xfs-conv/sda1]
 1513 ?        S<     0:00  \_ [xfs-cil/sda1]
 1514 ?        S<     0:00  \_ [xfs-reclaim/sda]
 1515 ?        S<     0:00  \_ [xfs-log/sda1]
 1516 ?        S<     0:00  \_ [xfs-eofblocks/s]
 1517 ?        S      0:51  \_ [xfsaild/sda1]
 1518 ?        S<     0:07  \_ [kworker/0:1H]
 1603 ?        S<     0:04  \_ [kworker/1:1H]
 2429 ?        S<     0:00  \_ [cfg80211]
 2640 ?        S<     0:00  \_ [iwl3945]
 2703 ?        S<     0:00  \_ [kvm-irqfd-clean]
 3135 ?        S<     0:00  \_ [xfs-buf/sda2]
 3136 ?        S<     0:00  \_ [xfs-data/sda2]
 3137 ?        S<     0:00  \_ [xfs-conv/sda2]
 3138 ?        S<     0:00  \_ [xfs-cil/sda2]
 3139 ?        S<     0:00  \_ [xfs-reclaim/sda]
 3140 ?        S<     0:00  \_ [xfs-log/sda2]
 3141 ?        S<     0:00  \_ [xfs-eofblocks/s]
 3142 ?        S      0:02  \_ [xfsaild/sda2]
23452 ?        S      0:00  \_ [scsi_eh_2]
23453 ?        S<     0:00  \_ [scsi_tmf_2]
23454 ?        S      0:44  \_ [usb-storage]
 4785 ?        S      0:01  \_ [kworker/u4:18]
 4843 ?        S      0:00  \_ [kworker/u4:76]
 5576 ?        S      0:00  \_ [kworker/1:2]
 6274 ?        S      0:08  \_ [kworker/0:3]
 6337 ?        S      0:02  \_ [kworker/1:0]
 6411 ?        S      0:02  \_ [kworker/0:0]
 6474 ?        S      0:00  \_ [kworker/0:1]
 6525 ?        S      0:00  \_ [kworker/0:2]
    1 ?        Ss     0:43 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
 1587 ?        Ss     0:11 /usr/lib/systemd/systemd-journald
 1608 ?        Ss     0:00 /usr/sbin/lvmetad -f
 1620 ?        Ss     0:01 /usr/lib/systemd/systemd-udevd
 3163 ?        S<sl   0:02 /sbin/auditd
 3187 ?        Ss     0:10 /usr/lib/systemd/systemd-logind
 3188 ?        Ssl    0:14 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidf
 3208 ?        Ssl    0:02 /usr/lib/polkit-1/polkitd --no-debug
 3209 ?        Ss     6:41 /usr/sbin/irqbalance --foreground
 3252 ?        Ssl    0:02 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
 3257 ?        Ss     0:05 /usr/sbin/crond -n
 3268 ?        S      0:04 /usr/sbin/chronyd
 3273 ?        Ss     0:00 login -- root
 4988 tty1     Ss+    0:00  \_ -bash
 3302 ?        Ssl    0:49 /usr/sbin/NetworkManager --no-daemon
 4941 ?        S      0:00  \_ /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/
 3607 ?        Ss     0:13 /usr/sbin/wpa_supplicant -u -f /var/log/wpa_supplicant.log -c /etc
 3813 ?        Ssl    5:49 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
 3815 ?        Ssl   79:38 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/do
 4224 ?        Ssl   50:51  \_ /usr/bin/docker-containerd-current -l unix:///var/run/docker/l
 3816 ?        Ssl    2:26 /usr/sbin/rsyslogd -n
 4217 ?        Ss     0:15 /usr/libexec/postfix/master -w
 4219 ?        S      0:02  \_ qmgr -l -t unix -u
 6450 ?        S      0:00  \_ pickup -l -t unix -u
10647 ?        Ss     0:00 /usr/sbin/sshd -D
 6501 ?        Ss     0:00  \_ sshd: root@pts/0
 6504 pts/0    Ss     0:00      \_ -bash
 6536 pts/0    R+     0:00          \_ ps fax
 

An important piece of information to get out of the ps  command is the PID. Many tasks require the PID to operate, and that is why a command like ps aux | grep dd, which will show process details about dd, including its PID, is quite common. An alternative way to get the same result is to use the pgrep  command. Use pgrep dd  to get a list of all PIDs that have a name containing the string dd.

Adjusting Process Priority with nice

As we have now multicore servers, nice is not longer that important as it was in the past.

When Linux processes are started, they are started with a specific priority. By default, all regular processes are equal and are started with the same priority, which is the priority number 20. In some cases, it is useful to change the default priority that was assigned to the process when it was started. You can do that using the nice  and renice  commands. Use nice  if you want to start a process with an adjusted priority. Use renice  to change the priority for a currently active process.

Alternatively, you can use the r  command from the top utility to change the priority of a currently running process.

Changing process priority may make sense in two different scenarios. Suppose, for example, that you are about to start a backup job that does not necessarily have to finish fast. Typically, backup jobs are rather resource intensive, so you might want to start it in a way that it is not annoying other users too much, by lowering its priority.

Another example is where you are about to start a very important calculation job. To ensure that it is handled as fast as possible, you might want to give it an increased priority, taking away CPU time from other processes.

On earlier Linux versions, it could be dangerous to increase the priority of one job too much, because other processes (including vital kernel processes) might risk being blocked out completely. On current Linux kernels, the situation is not that urgent anymore:

When using nice  or renice  to adjust process priority, you can select from values ranging from -20 to 19. The default niceness of a process is set to 0 (which results in the priority value of 20). By applying a negative niceness, you increase the priority. Use a positive niceness to decrease the priority. It is a good idea not to use the ultimate values immediately. Instead, use increments of 5 and see how it affects the application.

 

TIP:

Do not set process priority to -20; it risks blocking other processes from getting served.

 

Letís take a look at examples of how to use nice  and renice. The command nice -n 5 dd if=/dev/zero of=/dev/null &  starts an infinite I/O-intensive job, but with an adjusted niceness so that some place remains for other processes as well. To adjust the niceness of a currently running process, you need the PID of that process. The following two commands show how ps aux  is used to find the PID of the dd job from the previous example. Next, you see how the renice  command is used to change the niceness of that command:

1.  Use ps aux | grep dd  to find the PID of the dd command that you just started. The PID is in the second column of the command output.

2.  Use renice -n 10 -p 1234  (assuming that 1234 is the PID you just found).

Note that regular users can only decrease the priority of a running process. You must be root to give processes increased priority.

Sending Signals to Processes with kill, killall, and pkill

Before starting to think about using the kill  command or sending other signals to processes, it is good to know that Linux processes have a hierarchical relationship. Every process has a parent process, and as long as it lives, the parent process is responsible for the child processes it has created. This is particularly important when processes are terminated, because it is the parent that has to clean up the resources that were used by the children. When using kill on a parent process that still has active children, you will for that reason not just kill the parent process in question but also all of its currently active child processes.


Note

Over time, many different utilities have been developed for process monitoring and managing. I apologize if your favorite tool is not mentioned here. However, it does not really make sense to try to know all the available tools, because many tools offer comparable functionality.


The Linux kernel allows many signals to be sent to processes. Use man 7 signals  for a complete overview of all the available signals. Three of these signals work for all processes:

To send a signal to a process, the kill  command is used. The most common use is the need to stop a process, which you can do by using the kill  command followed by the PID of the process. This sends the SIGTERM signal to the process, which normally causes the process to cease its activity.

Sometimes the kill  command does not work because the process you want to kill is busy. In that case, you can use kill -9  to send the SIGKILL signal to the process. Because the SIGKILL signal cannot be ignored, it forces the process to stop, but you also risk losing data while using this command. In general, it is a bad idea to use kill -9:


Tip

Use kill -l  to show a list of available signals that can be used with kill.


There are some commands that are related to kill: killall  and pkill. The pkill  command is a bit easier to use because it takes the name rather than the PID of the process as an argument. You can use the killall  command if multiple processes using the same name need to be killed simultaneously.

Using killall  was particularly common when Linux environments were multiprocessing instead of multithreading. In a multiprocessing environment where a server starts several commands, all with the same name, it is not easy to stop these commands one by one based on their individual PID. Using killall  enables you to terminate all these processes simultaneously.


In a multithreaded environment, the urge to use killall  is smaller. Because there is often just one process that is generating several threads, all these threads are terminated anyway by stopping the process that started them. You still can use killall, though, to terminate lots of processes with the same name that have been started on your server.


Managing Processes from the Command Line

In this exercise, you learn how to work with ps, nice, kill, and related utilities to manage processes.

1.  Open a root shell. From this shell, type dd if=/dev/zero of=/dev/null &. Repeat this command three times.

2.  Type ps aux | grep dd. This shows all lines of output that have the letters dd in them; you will see more than just the dd processes, but that should not really matter. The processes you just started are listed last.

3.  Use the PID of one of the dd processes to adjust the niceness, using renice -n 5 <PID>. Notice that in top you cannot easily get an overview of processes and their current priority.

4.  Type ps fax | grep -B5 dd. The -B5  option shows the matching lines, including the five lines before that. Because ps fax  shows hierarchical relationships between processes, you should also find the shell and its PID from which all the dd processes were started.

5.  Find the PID of the shell from which the dd processes were started and type kill -9 <PID>, replacing <PID>  with the PID of the shell you just found. As the dd processes were started as background processes, they are not killed when their parent shell was killed. Instead, they have been moved upwards and are now a child of the systemd process.

Using top to Manage Processes

A convenient tool to manage processes is top. For common process management tasks, top is so great because it gives an overview of the most active processes currently running (hence the name top). This enables you to easily find processes that might need attention. From top, you can also perform common process management tasks, such as adjusting the current process priority and killing processes.

Among the information that you can conveniently obtain from the top utility is the process state.

Now that you know how to use the kill  and nice  commands from the command line, using the same functionality from top is even easier. From top, type k. top will then prompt for the PID of the process you want to send a signal to. By default, the most active process is selected. After you enter the PID, top asks which signal you want to send. By default, signal 15 for SIGTERM is used. However, if you want to insist a bit more, you can type 9  for SIGKILL. Now press Enter  to terminate the process.

To renice a running process from top, type r. You are first prompted for the PID of the process you want to renice. After entering the PID, you are prompted for the nice value you want to use. Enter a positive value to increase process priority or a negative value to decrease process priority.

 Linux Process States Overview

Most processes are in one of the following two states:

Only one process can run at a time on a single CPU. All other processes have to wait or be in some other state. This is why a process that is not running appears in a different state. States include the following:

In this example of a common process life cycle, the process is as follows:

  1. Born or forked
  2. Ready to run or runnable
  3. Running in user space or running in kernel space
  4. Blocked, Waiting, Sleeping, in an "Interruptible Sleep", or in an "Uninterruptable sleep"
  5. The process is sleeping, but it is present in main memory
  6. The process is sleeping, but it is present in secondary memory storage (swap space on disk)
  7. Terminated or stopped

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites

Top



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Haterís Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2018 by Dr. Nikolai Bezroukov. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) in the author free time and without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 12, 2019