Red Hat Certified System Administration 8



Chapter 10: Managing Processes


Goal

Evaluate and control processes running on a Red Hat Enterprise Linux system.

Sommaire

  • Processes
  • Jobs
  • Signals


Processes


Processes

Definition

Qu’est ce qu’un processus?
Programme en cours d’execution en un point de la machine

First process

■ All processes are descendants of the first system process, which is systemd(1) on a Red Hat Enterprise Linux 7 system
■ An existing (parent) process duplicates its own address space (fork) to create a new (child) process
■ Every new process is assigned a unique process ID (PID)

A major distinction can be made between two process types:

■ Shell jobs are commands started from the command line. They are associated with the shell that was current when the process was started. Shell jobs are also referred to as interactive processes.

■ Daemons are processes that provide services. They normally are started when a computer is booted and often (but certainly not in all cases) they are running with root privileges.

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 offers tools to manage individual threads. Thread management should be taken care of from within the command.

Processes States

  • Running or Runnable
  • Sleeping or waiting
  • Stopped
  • Zombie


Process state (S) displays as:

D = Uninterruptable Sleeping
R = Running or Runnable
S = Sleeping
T = Stopped or Traced
z = Zombie

Zombie

Il s'agit d'un état de processus impossible à gérer car le parent du processus zombie a disparu et l'enfant existe toujours,
mais ne peut plus être géré car le parent est nécessaire pour gérer ce processus.
Un processus zombie est normalement le résultat d'une mauvaise programmation.
Heureusement, les processus de zombies disparaîtront d'eux-mêmes. Parfois ils ne le font pas, et cela peut être un ennui.
Dans ce cas, la seule façon de nettoyer vos processus zombie actuels sont en redémarrant votre serveur.

Processes Functions

  • fork
  • exec
  • wait
  • exit

Commands

[root@serverX -]# top show process [+]
[root@serverX -]# top –p PID show process details
[root@serverX -]# top –p 1 show process details of PID 1 Process
[root@serverX -]# ps User manual process status
[root@serverX -]# ps aux all executed process since system starting status
[root@serverX -]# ps -ef See the Exact Command Used to Start Processes

[root@redhat -]# nohup cmd allow process to execute after terminal close
[root@redhat -]# pstree Display Processes tree
[root@redhat -]# pidof Command Display The command PID

Processes Priorities

Nice Priority

There are exactly 40 different levels of niceness a process can have. These nice levels range from -20 to 19. By default, processes will inherit their nice level from their parent, which is usually 0.

It is the CPU scheduling priority, higher vales (+19) mean lower priority, and lower values (-20) mean higher priority (inverse relationship).

Displaying nice levels with top

top NI field display nice levels of processes

The top command can be used to interactively view (and manage) processes. In a default configuration, top will display two columns of interest to the nice level: NI with the actual nice level, and PR, which displays the nice level as mapped to a larger priority queue, with a nice level of -20 mapping to a priority of 0 and a nice level of +19 mapping to a priority of 39.

PR = NI + 20



Displaying nice levels with ps

[root@redhat ~]# ps axo pid,comm,nice --sort=-nice

Launching processes with a different nice level

Whenever a process is started, it will normally inherit the nice level from its parent. This means that when a process is started from the command line, it will get the same nice level as the shell process that it was started from. In most cases, this will result in new processes running with a nice level of 0.

Unprivileged users are only allowed to set a positive nice level (0 to 19) . Only root can set a negative nice level (-20 to -1).

nice Command

Syntax

[root@serverX -]# nice cmd start process with nice level of 10
[root@serverX -]# nice -n nice-level cmd start process with a specific nice level
[root@serverX -]# nice - nice-level cmd start process with a specific nice level

Examples

[root@serverX -]# nice firefox start firefox process with nice level of 10
[root@serverX -]# nice - -9 firefox start process and change priority to -9
[root@serverX -]# nice -n 5 firefox start Firefox process with nice level 5
[root@serverX -]# nice -n -15 firefox start Firefox process with nice level 15

renice Command

[root@serverX -]# renice - nice-level PID change running process priority to a nice level
[root@serverX -]# renice -n nice-level PID change running process priority to a nice level
[root@serverX -]# renice - -9 38877 change running process priority to -9

PR Priority

PR = NI + 20

There are a total of 140 priorities and two distinct priority ranges implemented in Linux. The first one is a nice value (niceness) which ranges from -20 (highest priority value) to 19 (lowest priority value) and the default is 0, this is what we will uncover in this guide. The other is the real-time priority, which ranges from 1 to 99 by default, then 100 to 139 are meant for user-space.

But if you see a rt rather than a number as shown in the screenshot below, it basically means the process is running under real-time scheduling priority.


Jobs


Jobs

Managing Shell Jobs

When a user types a command, 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.

Job control is a command shell feature allowing a single shell instance to run and manage multiple commands. Without job control, apparent shell forks a child process to run a command, sleeping until the child process exits. When the shell prompt redisplays, the parent shell has returned. With job control, commands can be selectively suspended, resumed, and run asynchronously, allowing the shell to return for additional commands while child processes run.

■ Any command can be started in the background by appending an ampersand (&) to the command line.
■ The bash shell displays a job number (unique to the session) and the PID of the new child process.
■ The command shell does not wait for the child process and redisplays the shell prompt.

Background jobs can reconnect to the controlling terminal by being brought to the foreground
Using the fg command with the job ID(%job number).

Syntax
[user1@redhat ~]$ fg %Job-ID

Example
[student@localhost ~]$ fg %2
[student@localhost ~]$ fg %1


The ps option j displays job information, including the initial command shell of each session.
[student@localhost ~]$ ps j

To restart the process in the background, use the bg command with the same job ID.
[student@localhost ~]$ bg %2

Application 1

[student@localhost ~]$ ls -l &
[student@localhost ~]$ top &
[student@localhost ~]$ sleep 100000 &
[student@localhost ~]$ sleep 200000 &
[student@localhost ~]$ jobs

[student@localhost ~]$ fg %2
[student@localhost ~]$ fg %1


Application 2

[student@localhost ~]$ firefox &
[student@localhost ~]$ sleep 100000 &
[student@localhost ~]$ sleep 200000 &
[student@localhost ~]$ jobs

[student@localhost ~]$ fg %2
[student@localhost ~]$ fg %1



Signals


Signals

Le système et les processus communiquent entre eux en utilisant des signaux (SIGTERM, SIGKILL,…)
ces signaux ont des chiffres représentatifs qu’on peut aussi utiliser.
La commande suivante montre la liste des signaux et leurs chiffres


Commands

[root@serverX -]# kill –l show signal and their numbers
[root@serverX -]# kill -signal PID stop process with a signal
[root@serverX -]# kill -15 PID stop process and empty it in memory (SIGTERM)
[root@serverX -]# kill -9 PID stop process but not empty it in memory (SIGKILL)
[root@serverX -]# kill -1 PID stop process when terminal close (SIGKILL)

[root@serverX -]# killall -SIGTERM httpd would send the SIGTERM signal to all active httpd processes

[root@serverX -]# pkill - u user : Logging users out administratively
[root@serverX -]# pkill - SIGKILL - u bob

[root@serverX -]# w -f
[student@localhost ~]$ pgrep -l -u student

[student@localhost ~]$ pstree -p student : Display user Process Tree


Eninam BAGNABANA     |         |     bagnadavid@outlook.fr     |