Skip to main content

Processes and tasks


Table of contents

  1. What is a process?
  2. Viewing processes
  3. Foreground and Background
  4. Terminating processes
  5. Priorities: nice and renice
  6. Hands-on exercises


1 - What is a process?

Definition

A process is an instance of a program that is running.

Characteristics of a process

AttributeDescription
PIDProcess ID - unique identifier
PPIDParent PID - parent process
UIDOwner user
StateRunning, Sleeping, Stopped, Zombie
PriorityNice value (-20 to 19)

Process states

StateCodeDescription
RunningRCurrently running
SleepingSWaiting for an event
Disk sleepDWaiting for I/O (uninterruptible)
StoppedTStopped (STOP signal)
ZombieZTerminated, waiting for the parent

🔝 Back to table of contents



2 - Viewing processes

ps - Snapshot of processes

# Processus du terminal courant
ps

# Tous les processus (style BSD)
ps aux

# Tous les processus (style UNIX)
ps -ef

# Arborescence des processus
ps axjf
# ou
ps -ef --forest

Columns of ps aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root 1 0.0 0.1 169512 13256 ? Ss Jan10 0:12 /sbin/init
john 1234 0.5 2.3 456789 45678 pts/0 S+ 10:30 0:05 vim file.txt
ColumnDescription
USEROwner
PIDProcess ID
%CPUCPU usage
%MEMMemory usage
VSZVirtual memory (KB)
RSSResident memory (KB)
TTYAssociated terminal
STATProcess state
STARTStart time
TIMECumulative CPU time
COMMANDCommand

top - Real-time monitoring

top

Navigation in top:

KeyAction
qQuit
kKill a process (asks for PID)
rRenice (change priority)
MSort by memory
PSort by CPU
1Show all CPUs
cShow full command
fChoose the columns

htop - An improved top

# Installer si nécessaire
sudo apt install htop

# Lancer
htop

Advantages of htop:

  • Colored interface
  • Mouse navigation
  • Process tree (F5)
  • Easy filtering (F4)
  • Direct actions (F9 to kill)

Useful commands

# Compter les processus
ps aux | wc -l

# Processus d'un utilisateur
ps -u john

# Processus par utilisation CPU
ps aux --sort=-%cpu | head -10

# Processus par utilisation mémoire
ps aux --sort=-%mem | head -10

# Trouver un processus spécifique
ps aux | grep nginx
pgrep nginx
pgrep -l nginx # Avec le nom

🔝 Back to table of contents



3 - Foreground and Background

Concepts

ModeDescriptionInteraction
ForegroundForegroundBlocks the terminal
BackgroundBackgroundTerminal free

Launch in the background

# Avec & à la fin
sleep 100 &
# [1] 12345 (numéro de job et PID)

# Commande longue en background
./script_long.sh &

Control jobs

# Lancer une commande
sleep 300

# Suspendre avec Ctrl+Z
^Z
# [1]+ Stopped sleep 300

# Voir les jobs
jobs
# [1]+ Stopped sleep 300

# Reprendre en background
bg %1
# ou juste
bg

# Reprendre en foreground
fg %1
# ou juste
fg

Typical workflow

nohup - Survive disconnection

# Sans nohup : le processus meurt à la déconnexion
./script.sh &

# Avec nohup : survit à la déconnexion
nohup ./script.sh &
# La sortie va dans nohup.out

# Avec redirection personnalisée
nohup ./script.sh > output.log 2>&1 &

disown - Detach a process

# Lancer puis détacher
./script.sh &
disown

# Ou directement
./script.sh & disown

🔝 Back to table of contents



4 - Terminating processes

kill - Send signals

# Terminer proprement (SIGTERM - signal 15)
kill 12345
kill -15 12345

# Forcer l'arrêt (SIGKILL - signal 9)
kill -9 12345

# Suspendre (SIGSTOP)
kill -STOP 12345

# Reprendre (SIGCONT)
kill -CONT 12345

Common signals

SignalNumberEffect
SIGTERM15Terminate cleanly (default)
SIGKILL9Kill immediately (force)
SIGHUP1Reload configuration
SIGSTOP19Suspend
SIGCONT18Resume
SIGINT2Interrupt (Ctrl+C)

killall and pkill

# Tuer par nom de processus
killall nginx
killall -9 firefox

# Avec pkill (plus flexible)
pkill nginx
pkill -9 nginx

# Tuer les processus d'un utilisateur
pkill -u john

# Tuer par pattern
pkill -f "python script.py"

Practical example

# 1. Trouver le PID
pgrep nginx
# ou
ps aux | grep nginx

# 2. Terminer proprement
kill 12345

# 3. Si ne répond pas, forcer
kill -9 12345

# Ou en une ligne
pkill -9 nginx
warning

kill -9 should be a last resort! The process does not have time to clean up (temporary files, connections, etc.).

🔝 Back to table of contents



5 - Priorities: nice and renice

The concept of priority

The "nice" value determines the priority of a process:

NicePriorityUsage
-20Very highCritical processes
0NormalDefault
+19Very lowBackground tasks

nice - Launch with a priority

# Lancer avec priorité basse (gentil avec les autres)
nice -n 10 ./script.sh

# Lancer avec priorité haute (nécessite root)
sudo nice -n -10 ./processus_important

renice - Modify the priority

# Changer la priorité d'un processus existant
renice 10 -p 12345

# Baisser la priorité (utilisateur normal peut augmenter nice)
renice 15 -p 12345

# Augmenter la priorité (nécessite root)
sudo renice -5 -p 12345

# Changer pour tous les processus d'un utilisateur
sudo renice 5 -u john

View the priority

# Dans top/htop : colonne NI
top

# Avec ps
ps -eo pid,ni,comm

Use cases

SituationAction
Backup at nightnice -n 19 backup.sh
Compilationnice -n 10 make
Critical processsudo nice -n -10 service

🔝 Back to table of contents



6 - Hands-on exercises

Exercise 1: Observation

# 1. Lancez htop
htop
# Observez les processus, triez par CPU (F6)

# 2. Dans un autre terminal, identifiez les 5 processus
# les plus gourmands en CPU
ps aux --sort=-%cpu | head -6

# 3. Identifiez les 5 plus gourmands en mémoire
ps aux --sort=-%mem | head -6

Exercise 2: Background/foreground management

# 1. Lancez une commande longue
sleep 300

# 2. Suspendez-la
# Appuyez sur Ctrl+Z

# 3. Vérifiez les jobs
jobs

# 4. Relancez en background
bg

# 5. Vérifiez qu'elle tourne
jobs
ps aux | grep sleep

# 6. Ramenez en foreground et terminez
fg
# Puis Ctrl+C

Exercise 3: Kill and signals

# 1. Lancez un processus en background
sleep 600 &
# Notez le PID affiché

# 2. Vérifiez qu'il tourne
ps aux | grep sleep

# 3. Envoyez SIGSTOP pour le suspendre
kill -STOP <PID>

# 4. Vérifiez son état (T = Stopped)
ps aux | grep sleep

# 5. Reprenez avec SIGCONT
kill -CONT <PID>

# 6. Terminez-le proprement
kill <PID>

Quiz

Q1. Which signal forces immediate termination?

Answer

SIGKILL (signal 9): kill -9 PID

Q2. How do you launch a command in the background?

Answer

Add & at the end: commande &

Q3. Which command displays processes in real time?

Answer

top or htop

🔝 Back to table of contents



Key takeaways

  • A process = running program (unique PID)
  • ps aux = snapshot, top/htop = real time
  • & launches in the background, Ctrl+Z suspends
  • bg resumes in the background, fg in the foreground
  • kill sends SIGTERM, kill -9 forces
  • nice at launch, renice afterwards
  • nohup to survive disconnection

🔝 Back to table of contents


← Previous chapter | Next chapter: Services and systemd →