NANDHOO.

Chapter 11: Process Management

Linux is a multitasking operating system, meaning it runs many programs (processes) simultaneously. As a system administrator or developer, you must know how to monitor these processes, manage their resources, and terminate them when they misbehave.

I. Understanding Processes

A Process is an instance of a running program. Each process is assigned a unique Process ID (PID).

1. The Process Hierarchy

Every process has a parent. The first process started by the kernel is systemd (PID 1), which is the ancestor of all other processes on the system.

2. Viewing Processes

  • ps aux: A static snapshot of all processes.
    • a: All users.
    • u: User-oriented format.
    • x: Processes not attached to a terminal.
  • top / htop: Dynamic, real-time views. htop is preferred for its interactivity.

II. Unix Signals

Signals are software interrupts sent to a process to trigger a specific behavior.

SignalNameCommandPurpose
1SIGHUPkill -1Hangup (Reload configuration).
2SIGINTCtrl+CInterrupt (Graceful stop).
9SIGKILLkill -9Force Kill (Cannot be ignored).
15SIGTERMkill -15Terminate (Default, polite request to stop).
19SIGSTOPCtrl+ZPause the process.

III. Job Control: Multitasking in One Terminal

You can run multiple programs in a single terminal session using job control.

FOREGROUNDCtrl+ZSTOPPEDbg %1BACKGROUNDfg %1

  • command &: Starts the command directly in the background.
  • jobs: Lists all background tasks in the current shell.
  • fg %1: Brings job #1 back to the foreground.
  • bg %1: Resumes job #1 in the background.

IV. Process Priority (Niceness)

The kernel decides how much CPU time each process gets based on its Niceness value, ranging from -20 (highest priority) to 19 (lowest priority).

  • nice: Start a program with a specific priority.
    • nice -n 10 backup.sh (Runs with lower priority to save CPU for other tasks).
  • renice: Change the priority of a process that is already running.
    • renice +5 -p 1234.

V. System Load and Uptime

  • uptime: Shows how long the system has been running and the Load Average.
  • Load Average Breakdown: Shows the average number of processes waiting for CPU over the last 1, 5, and 15 minutes.
    • If the load is higher than the number of CPU cores, the system is over-saturated.

In the next chapter, we'll learn how to schedule these processes to run automatically using Cron and Automation.