Processes and tasks
Table of contents
- What is a process?
- Viewing processes
- Foreground and Background
- Terminating processes
- Priorities: nice and renice
- Hands-on exercises
1 - What is a process?
Definition
A process is an instance of a program that is running.
Characteristics of a process
| Attribute | Description |
|---|---|
| PID | Process ID - unique identifier |
| PPID | Parent PID - parent process |
| UID | Owner user |
| State | Running, Sleeping, Stopped, Zombie |
| Priority | Nice value (-20 to 19) |
Process states
| State | Code | Description |
|---|---|---|
| Running | R | Currently running |
| Sleeping | S | Waiting for an event |
| Disk sleep | D | Waiting for I/O (uninterruptible) |
| Stopped | T | Stopped (STOP signal) |
| Zombie | Z | Terminated, 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
| Column | Description |
|---|---|
| USER | Owner |
| PID | Process ID |
| %CPU | CPU usage |
| %MEM | Memory usage |
| VSZ | Virtual memory (KB) |
| RSS | Resident memory (KB) |
| TTY | Associated terminal |
| STAT | Process state |
| START | Start time |
| TIME | Cumulative CPU time |
| COMMAND | Command |
top - Real-time monitoring
top
Navigation in top:
| Key | Action |
|---|---|
q | Quit |
k | Kill a process (asks for PID) |
r | Renice (change priority) |
M | Sort by memory |
P | Sort by CPU |
1 | Show all CPUs |
c | Show full command |
f | Choose 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
| Mode | Description | Interaction |
|---|---|---|
| Foreground | Foreground | Blocks the terminal |
| Background | Background | Terminal 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
| Signal | Number | Effect |
|---|---|---|
| SIGTERM | 15 | Terminate cleanly (default) |
| SIGKILL | 9 | Kill immediately (force) |
| SIGHUP | 1 | Reload configuration |
| SIGSTOP | 19 | Suspend |
| SIGCONT | 18 | Resume |
| SIGINT | 2 | Interrupt (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:
| Nice | Priority | Usage |
|---|---|---|
| -20 | Very high | Critical processes |
| 0 | Normal | Default |
| +19 | Very low | Background 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