Skip to content

13. Process Management

When you run a program or a command, the Linux kernel loads it into memory and tracks it as a Process.

  • PID (Process ID): A unique numeric identifier assigned to every running process.
  • PPID (Parent Process ID): The PID of the process that spawned this process. If you open a shell (PID 100) and type ls, the ls command is given a new PID, and its PPID is 100.
  • PID 1: The very first process that starts when a Linux machine boots. Modern systems use systemd as PID 1. If PID 1 dies, the entire system crashes (Kernel Panic). In Docker containers, the application you run acts as PID 1!

A process isn’t always actively running on the CPU. It shifts through states:

  • Running (R): Currently executing on the CPU.
  • Sleeping (S): Waiting for an event (like user input or disk read) to finish.
  • Stopped (T): Paused (usually by a user sending a stop signal).
  • Zombie (Z): A process that has finished executing, but its parent hasn’t yet collected its exit status. (It uses a PID slot but no CPU/RAM).

You need tools to view the running processes and their resource consumption.

  • ps (Process Status): Takes a static snapshot of currently running processes.
    • ps aux (Displays all processes running on the system, for all users, in a detailed format).
  • top: A real-time, constantly updating table of processes, sorted by default by CPU usage.
  • htop: A more modern, interactive, and colorful version of top (often needs to be installed via apt install htop).
  • pgrep: Searches for processes by name and returns their PID.
    • pgrep nginx (Outputs 1024, 1025, etc.)

You communicate with running processes by sending them Signals.

  • SIGTERM (15 - Terminate): The default signal. Asks the process to shut down gracefully (save data, close connections, then exit).
  • SIGKILL (9 - Kill): The nuclear option. The kernel immediately terminates the process without giving it a chance to clean up. Use only when a process is completely frozen.
  • SIGINT (2 - Interrupt): Sent when you press Ctrl + C in the terminal to cancel a running foreground command.
  • SIGHUP (1 - Hangup): Originally meant “the terminal disconnected.” Today, it is commonly used to tell background services (like Nginx) to elegantly reload their configuration files without dropping traffic.
  • kill: Sends a signal to a specific PID.
    • kill 1024 (Sends a graceful SIGTERM)
    • kill -9 1024 (Sends a forceful SIGKILL)
  • pkill: Sends a signal to a process by its name.
    • pkill nginx
  • killall: Similar to pkill, but strictly matches the exact name of the process.

13.4 Job Management (Foreground vs Background)

Section titled “13.4 Job Management (Foreground vs Background)”

When you run a command in the terminal, it normally runs in the Foreground, meaning your terminal is blocked until the command finishes.

To run a command without blocking the terminal, append an & (Ampersand).

Terminal window
sleep 300 &
# Output: [1] 1045 (Job ID 1, PID 1045)

If you are running a long command in the foreground and want to pause it, press Ctrl + Z. This sends a SIGTSTP (Stop) signal, moving it to the background in a “Stopped” state.

  • jobs: Lists all background and suspended jobs attached to your current terminal.
  • bg (Background): Resumes a stopped job, but keeps it running in the background.
    • bg %1 (Resumes Job #1)
  • fg (Foreground): Brings a background job back to the foreground so you can interact with it.
    • fg %1

If you start a background job and close your SSH session, the job will receive a SIGHUP and terminate. To prevent this, use nohup (No Hangup).

Terminal window
nohup ./long_script.sh &

All output will be automatically redirected to a file named nohup.out so you can review it later.