Learning Objectives
- Understand processes, PIDs, and common process states.
- Inspect running processes with ps, top, and related tools.
- Send signals safely with kill, pkill, and killall.
- Manage services with systemctl and read logs with journalctl.
Prerequisites
Complete Chapters 1–5 before starting this chapter.
1. Introduction
Every application you run becomes one or more processes. Linux manages these processes, allocates CPU time, assigns memory and coordinates communication between applications and the kernel.
2. Program vs Process
A program is an executable file stored on disk. A process is a running instance of that program.
Program ──► Execute ──► Running Process (Parent ──► Child)
3. Process Lifecycle
- Program starts.
- Kernel assigns a Process ID (PID).
- Process requests CPU and memory.
- Process finishes or is terminated.
4. Process States
| State | Description |
|---|---|
| R | Running or ready to run. |
| S | Interruptible sleep. |
| D | Uninterruptible sleep (usually waiting on I/O operations). |
| T | Stopped or traced. |
| Z | Zombie process. |
5. Understanding PID & PPID
Audit process structures via ps -ef:
| Column | Meaning |
|---|---|
| UID | Owner of the process. |
| PID | Process ID. |
| PPID | Parent Process ID. |
| TTY | Terminal associated with the process. |
| TIME | CPU time used. |
| CMD | Executed command. |
6. Viewing Running Processes
Common inspection utilities:
ps: With no options, list only the processes attached to your current terminal session.ps -ef: List all processes using traditional UNIX options.ps aux: List all processes using BSD syntax mapping.pstree: Display parent-child process relationships as a clean visual tree.pgrep sshd: Find target process IDs by matching names quickly.pidof nginx: Show individual PIDs of a running software daemon.
7. Practical Examples
ps -ef | grep sshd
pgrep sshd
pidof systemd
pstree
These commands help locate services, verify whether applications are running and understand parent-child relationships.
8. Hands-on Practice
- Run
ps. - Display all processes using
ps -ef. - Compare the output of
ps aux. - Locate the SSH daemon using
pgrep. - Display the process tree using
pstree.
9. Common Mistakes
- Confusing a binary program with an active running process.
- Ignoring PPID variables while tracing runaway child tasks.
- Assuming every sleeping process requires a task termination.
- Killing a process without identifying its parent or purpose.
10. Process Signaling (kill, killall, pkill)
The kill command sends a signal to a specific process ID (PID). Despite its name, a signal is usually a message the process can handle, ignore, or catch — except SIGKILL and SIGSTOP, which the kernel enforces and the process cannot catch or ignore. Always send SIGTERM first to let the program shut down cleanly, and only escalate to SIGKILL if it will not stop.
| Signal | Purpose |
|---|---|
| SIGTERM (15) | Ask the process to terminate gracefully, allowing it to clean up. This is the default signal kill sends. |
| SIGKILL (9) | Force immediate termination in the kernel. The process cannot catch or ignore it and gets no chance to clean up. |
| SIGHUP (1) | Originally "hangup" (terminal disconnected). Many daemons are written to re-read their configuration on SIGHUP, but for a process that does not handle it the default action is to terminate. |
| SIGSTOP (19) | Pause (suspend) a process. It cannot be caught or ignored. |
| SIGCONT (18) | Resume a process that was stopped. |
| SIGTSTP (20) | Terminal stop, sent when you press Ctrl+Z. Like SIGSTOP but the process can handle it. |
kill 4521 # Send SIGTERM (the default) to PID 4521
kill -TERM 4521 # The same thing, stated explicitly
kill -HUP 4521 # SIGHUP: many daemons reload config; processes that ignore it may terminate
kill -9 4521 # Send SIGKILL as a last resort if SIGTERM did not work
pkill nginx # Signal processes whose name matches "nginx"
pkill -u alice # Signal all processes owned by user alice
killall nginx # Signal every process named exactly "nginx"
11. Shell Job Control & Priorities
Append & to a command to run it in the background so your shell prompt returns immediately. Press Ctrl+Z to suspend the foreground command (this sends SIGTSTP); then use bg to let it continue running in the background or fg to bring it back to the foreground. List the jobs started from your current shell with jobs. To keep a task running after you log out, start it with nohup command &. Adjust how much CPU time a process is given relative to others with nice (when starting it) and renice (for a process already running).
12. Service Control via systemd (systemctl)
Modern Linux systems use systemd as the first process (PID 1) to start services at boot and manage them while the system runs. You control individual services (called units) with systemctl:
systemctl status sshd # Show whether the service is running, plus recent log lines
systemctl start sshd # Start the service now
systemctl stop sshd # Stop the service now
systemctl restart sshd # Stop and start the service
systemctl reload sshd # Re-read configuration without a full restart (if supported)
systemctl enable sshd # Start the service automatically at boot
systemctl enable --now sshd # Enable at boot and start immediately
13. System Monitoring & Diagnostics
Watch live CPU and memory usage per process with top, or the friendlier, color-coded htop. Check overall system activity (CPU, memory, and I/O) with vmstat, and view free and used memory in human-readable units with free -h. See how long the system has been running and its 1-, 5-, and 15-minute load averages with uptime.
To understand how long the system took to boot and which units were slowest, use systemd-analyze:
systemd-analyze # Show total boot time (firmware, loader, kernel, userspace)
systemd-analyze blame # List each unit's startup time, slowest first
systemd-analyze critical-chain # Show the chain of units on the critical boot path
14. Centralized Log Inspection (journalctl)
Query the systemd journal with journalctl instead of opening individual log files by hand during an incident. For example, journalctl -u sshd -n 50 --no-pager shows the last 50 log lines for the sshd unit, and adding -f follows new entries as they arrive.
15. Production Troubleshooting Workflow
| Problem Context | Possible Cause | Useful Commands |
|---|---|---|
| High CPU load | Runaway process or busy thread | top, ps, renice |
| High Memory usage | Application memory leak | free -h, vmstat |
| Service Failed state | Configuration parameters error | systemctl status, journalctl -u |
Commands Covered in This Chapter
ps— list running processestop/htop— interactive CPU and memory viewkill/pkill/killall— send signals to processesnice/renice— adjust process prioritysystemctl— start, stop, enable, and check servicesjournalctl— query systemd journal logsfree/vmstat— check memory and system activitysystemd-analyze— inspect boot and unit timing