13. Process Management
13.1 The Anatomy of a Process
Section titled “13.1 The Anatomy of a Process”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 typels, thelscommand is given a new PID, and its PPID is100. - PID 1: The very first process that starts when a Linux machine boots. Modern systems use
systemdas PID 1. If PID 1 dies, the entire system crashes (Kernel Panic). In Docker containers, the application you run acts as PID 1!
Process States
Section titled “Process States”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).
13.2 Monitoring Commands
Section titled “13.2 Monitoring Commands”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 oftop(often needs to be installed viaapt install htop).pgrep: Searches for processes by name and returns their PID.pgrep nginx(Outputs1024,1025, etc.)
13.3 Signals (Controlling Processes)
Section titled “13.3 Signals (Controlling Processes)”You communicate with running processes by sending them Signals.
Important Signals
Section titled “Important 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 pressCtrl + Cin 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.
Sending Signals (Kill Commands)
Section titled “Sending Signals (Kill Commands)”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 topkill, 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.
Backgrounding a Job
Section titled “Backgrounding a Job”To run a command without blocking the terminal, append an & (Ampersand).
sleep 300 &# Output: [1] 1045 (Job ID 1, PID 1045)Suspending a Foreground Job
Section titled “Suspending a Foreground Job”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.
Managing Jobs
Section titled “Managing Jobs”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
Surviving Disconnects (nohup)
Section titled “Surviving Disconnects (nohup)”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).
nohup ./long_script.sh &All output will be automatically redirected to a file named nohup.out so you can review it later.