|

Linux CLI 21 🐧 view and terminate processes

Processes are the running applications that consume system resources like CPU and memory. Every process has a unique PID (Process ID), and can be viewed with ps or top, and terminated with kill or pkill.


What Are Processes?

A process is an instance of a running program. Every command you run creates a process.

Key concepts:

TermDescription
ProcessA running program consuming resources
PIDProcess ID — a unique number assigned at creation
Parent processThe process that started another process
Child processA process started by another process
ResourcesCPU, memory, disk I/O, network, etc.

Key point: A PID is assigned when the process is created and stays the same throughout its life.

Visual — parent and child processes:

┌──────────────────┐
│  bash (PID 100)  │  ← parent
└────────┬─────────┘
         │ starts
    ┌────┴────┐
    ▼         ▼
┌────────┐ ┌────────┐
│ls(PID  │ │vim(PID │  ← children
│ 101)   │ │ 102)   │
└────────┘ └────────┘

Commands Overview

CommandPurpose
psSnapshot of running processes
topReal-time process viewer
htopInteractive process viewer (better)
killTerminate a process by PID
pkillTerminate processes by name

The ps Command

Process Status — displays information about running processes.

ps aux
ps -u kronos
ps --pid=31009
CommandDescription
ps auxAll processes with detailed info
ps -u kronosProcesses owned by kronos
ps --pid=31009Info about a specific process
ps -efAlternative format (System V)
ps -eAll processes, simple format
ps aux | grep nginxFilter by name

Understanding ps aux Output

$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 168336 13212 ?        Ss   Jan14   0:03 /sbin/init
root       512  0.0  0.2  41208  8108 ?        Ss   Jan14   0:00 /usr/sbin/sshd
kronos    1234  0.5  1.2 512345 45678 pts/0    S+   10:30   0:02 vim notes.txt
kronos    5678  2.3  5.6 876543 98765 pts/1    R+   10:35   0:15 firefox
ColumnMeaning
USERUser who owns the process
PIDProcess ID
%CPUCPU usage percentage
%MEMMemory usage percentage
VSZVirtual memory size (KB)
RSSResident Set Size — physical memory (KB)
TTYTerminal the process is attached to
STATProcess state
STARTStart time/date
TIMETotal CPU time used
COMMANDCommand that started the process

The STAT Column — Process States

SymbolMeaning
RRunning — actively using CPU
SSleeping — waiting for an event
DDisk sleep — uninterruptible wait for I/O
ZZombie — finished but not reaped by parent
TStopped — paused (Ctrl+Z)
IIdle — idle kernel thread
<High priority
NLow priority
sSession leader
+Foreground process group

Common combinations:

Ss   →  Sleeping, session leader
R+   →  Running, foreground
S+   →  Sleeping, foreground
Z    →  Zombie

Filtering ps Output

By user:

$ ps -u kronos
PID TTY          TIME CMD
1234 pts/0    00:00:02 bash
5678 pts/0    00:00:00 vim

By PID:

$ ps --pid=1234
PID TTY          TIME CMD
1234 pts/0    00:00:02 vim

By name (with grep):

$ ps aux | grep nginx
root       812  0.0  0.1  ...  nginx: master process
www-data   813  0.0  0.1  ...  nginx: worker process

Just the name and PID:

$ ps -eo pid,comm
  PID COMMAND
    1 systemd
  512 sshd
 1234 bash

The top Command

A real-time process viewer that updates continuously.

top

Sample output:

top - 10:35:22 up 2 days,  3:15,  2 users,  load average: 0.52, 0.48, 0.45
Tasks: 245 total,   1 running, 244 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.2 us,  1.1 sy,  0.0 ni, 93.5 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  16384.0 total,   8124.3 free,   5123.4 used,   3136.3 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.  10214.0 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 5678 kronos    20   0  876543  98765  12345 R  15.3   5.6   1:32.45 firefox
 1234 kronos    20   0  512345  45678   5678 S   2.5   1.2   0:15.20 vim
    1 root      20   0  168336  13212   8765 S   0.0   0.1   0:03.12 systemd

Understanding the header:

  • load average — system load over 1, 5, 15 minutes
  • Tasks — total processes by state
  • %Cpu(s) — CPU breakdown (us=user, sy=system, id=idle, wa=wait)
  • MiB Mem — memory usage
  • MiB Swap — swap usage

Interactive Shortcuts in top

KeyAction
zToggle colors
1Toggle between single and per-CPU view
fSelect which fields to show
sSet update interval (seconds)
hShow help
qQuit top
kKill a process (prompts for PID)
MSort by memory usage
PSort by CPU usage
TSort by time
uFilter by user

Examples in action:

$ top
# Press z → colors on/off
# Press 1 → see each CPU core
# Press f → choose columns (add PIDs, user, etc.)
# Press s → set refresh to 2 seconds
# Press M → sort by memory
# Press q → exit

The kill Command

Sends a signal to a process by PID — usually to terminate it.

kill -9 PID
kill -15 PID
kill -l
CommandDescription
kill PIDSends SIGTERM (default, signal 15)
kill -9 PIDForce kill (SIGKILL)
kill -15 PIDNormal terminate (SIGTERM)
kill -lList all available signals
kill -SIGKILL PIDSame as -9 (named signal)
kill -SIGTERM PIDSame as -15
kill -1 PIDSIGHUP — reload configuration
kill -2 PIDSIGINT — same as Ctrl+C

Common Signals

NumberNameDescription
1SIGHUPHangup / reload config
2SIGINTInterrupt (Ctrl+C)
9SIGKILLForce kill (cannot be caught)
15SIGTERMNormal termination (default)
18SIGCONTContinue stopped process
19SIGSTOPStop process (cannot be caught)
20SIGTSTPStop process (Ctrl+Z)

Examples

Normal termination (graceful):

$ kill -15 5678
# or
$ kill 5678
# Process gets a chance to clean up

Force kill (immediate):

$ kill -9 5678
# Process is terminated immediately — no cleanup

Using signal names:

$ kill -SIGTERM 5678
$ kill -SIGKILL 5678

Reload config (for daemons):

$ kill -SIGHUP 812
# Many daemons reload their config on SIGHUP

List all signals:

$ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     ...

SIGTERM vs SIGKILL

AspectSIGTERM (15)SIGKILL (9)
Cleanup✅ Process can clean up❌ No cleanup
Can be caught?✅ Yes❌ No
Use caseNormal terminationForced termination
Preferred?✅ Try this first⚠️ Last resort

Best practice:

# Try graceful shutdown first
$ kill -15 5678
# Wait a few seconds
# If still running, force kill
$ kill -9 5678

The pkill Command

Process KILL — sends signals to processes by name (or other criteria).

pkill -1 name
pkill -9 name
pkill -15 name
pkill -9 -u user name
CommandDescription
pkill -1 nameReload a process (SIGHUP)
pkill -9 nameForce kill (SIGKILL)
pkill -15 nameTerminate normally (SIGTERM)
pkill -9 -u user nameKill by name for specific user

Examples

Kill by name:

$ pkill firefox
# Kills all processes named "firefox"

Force kill:

$ pkill -9 chrome

Reload a daemon:

$ pkill -1 nginx
# Reloads nginx configuration

Kill processes for a specific user:

$ pkill -9 -u alice firefox
# Only kills firefox processes owned by alice

Kill by pattern:

$ pkill -f "python.*script"
# -f matches against full command line

Common pkill Options

OptionDescription
-u userMatch by user
-fMatch against full command line
-xExact name match
-nNewest process
-oOldest process
-cCount matching processes
-eEcho what’s killed
-lList signal names

Complete Example Session

# ============================================
# PART 1: VIEWING PROCESSES
# ============================================

# All processes with detail
$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 168336 13212 ?        Ss   Jan14   0:03 /sbin/init
root       512  0.0  0.2  41208  8108 ?        Ss   Jan14   0:00 sshd
kronos    1234  0.5  1.2 512345 45678 pts/0    S+   10:30   0:02 vim

# Filter by user
$ ps -u kronos
PID TTY          TIME CMD
1234 pts/0    00:00:02 vim
5678 pts/0    00:00:00 bash

# Specific PID
$ ps --pid=1234
PID TTY          TIME CMD
1234 pts/0    00:00:02 vim

# Filter by name
$ ps aux | grep firefox
kronos    5678  2.3  5.6  ...  firefox

# ============================================
# PART 2: TOP COMMAND
# ============================================

$ top
# Press z → colors
# Press 1 → per-CPU view
# Press M → sort by memory
# Press P → sort by CPU
# Press u → filter by user
# Press k → kill a process
# Press h → help
# Press q → quit

# ============================================
# PART 3: KILL COMMAND
# ============================================

# Find the PID
$ ps aux | grep firefox
kronos    5678  2.3  5.6  ...  firefox

# Try graceful termination
$ kill -15 5678

# If still running, force kill
$ kill -9 5678

# Using signal names
$ kill -SIGTERM 5678
$ kill -SIGKILL 5678

# Reload a daemon
$ sudo kill -SIGHUP 812

# List all signals
$ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT  ...

# ============================================
# PART 4: PKILL COMMAND
# ============================================

# Kill by name
$ pkill firefox

# Force kill all chrome processes
$ pkill -9 chrome

# Reload nginx
$ sudo pkill -1 nginx

# Kill firefox for user alice only
$ pkill -9 -u alice firefox

# Match full command line
$ pkill -f "python.*script.py"

# Count matches without killing
$ pkill -c firefox
5

# See what would be killed
$ pkill -e firefox
firefox killed (pid 5678)
firefox killed (pid 5679)

# ============================================
# PART 5: PRACTICAL SCENARIOS
# ============================================

# Scenario 1: App is frozen
$ ps aux | grep firefox
kronos    5678  99.0  5.6  ...  firefox

$ kill -9 5678
# ✅ Force killed the frozen process

# Scenario 2: Kill all by name
$ pkill -9 chrome
# Kills all chrome processes at once

# Scenario 3: Restart a service gracefully
$ sudo pkill -1 nginx
# Nginx reloads config without dropping connections

# Scenario 4: Find and kill a runaway Python script
$ ps aux | grep python
kronos    9100  95.0  10.0  ...  python data_process.py

$ kill -15 9100
# Give it a chance to save state
# If ignored:
$ kill -9 9100

# Scenario 5: Filter processes by user
$ ps -u alice
$ ps aux | grep "^alice"

# Scenario 6: Real-time monitoring
$ top
# Watch CPU, memory, and process list update

# Scenario 7: Kill a process by pattern
$ pkill -f "java -jar myapp.jar"
# Kills the specific Java app

# Scenario 8: Zombie process
$ ps aux | grep defunct
user  1234  0.0  0.0  ...  [python ] <defunct>
# Zombie — needs to be reaped by parent process

Quick Reference

ps Options

OptionDescription
auxAll processes, detailed
-efAll processes (Unix style)
-u userFilter by user
--pid=PIDSpecific PID
-eAll processes

Process States (STAT)

SymbolMeaning
RRunning
SSleeping
DDisk sleep
ZZombie
TStopped
IIdle
<High priority

top Shortcuts

KeyAction
zColors
1Per-CPU view
fFields
sUpdate interval
hHelp
qQuit
kKill process
M / PSort by mem / CPU

Common Signals

NumberNamePurpose
1SIGHUPReload config
2SIGINTInterrupt (Ctrl+C)
9SIGKILLForce kill
15SIGTERMNormal terminate

Best Practices

Do This:

# Try SIGTERM first, then SIGKILL
kill -15 PID
sleep 3
kill -9 PID

# Use ps or top to find the PID
ps aux | grep firefox
# Then kill the specific PID

# Use pkill by name for convenience
pkill firefox

# Reload daemons with SIGHUP
sudo pkill -1 nginx

# Filter by user to avoid killing wrong processes
pkill -u alice firefox

# Check what would be killed before doing it
pkill -c firefox    # Count matches

Don’t Do This:

# Don't jump straight to -9
kill -9 PID        # ❌ Process can't clean up!
kill -15 PID       # ✅ Try this first

# Don't kill PID 1
kill -9 1          # ❌ Kills init — system crash!

# Don't pkill with common names
pkill -9 bash      # ❌ Kills your own shell!
pkill -9 python    # ⚠️ Kills ALL Python processes

# Don't use kill on your own shell
kill -9 $$         # ❌ Kills your current shell

# Don't ignore what ps tells you
ps aux | grep chrome
# Before killing, verify it's really chrome!

Common Pitfalls

PitfallProblemSolution
kill -9 firstNo cleanupTry -15 first
Killing PID 1System crashNever touch PID 1
pkill common nameKills unrelated processesUse -u or -f
Wrong PIDKills wrong processVerify with ps first
Zombie won’t dieParent hasn’t reapedRestart parent
kill on systemd serviceDoesn’t restartUse systemctl stop

Real-World Examples

1. Kill a Frozen App

# Find the frozen process
$ ps aux | grep firefox
kronos  5678  99.0  ...  firefox

# Try graceful shutdown
$ kill -15 5678

# Force kill if needed
$ kill -9 5678

2. Stop All Chrome Processes

$ pkill chrome
$ pkill -9 chrome

3. Reload Nginx Config

$ sudo pkill -1 nginx
# Nginx re-reads its config without dropping connections

4. Kill Runaway Python Script

$ ps aux | grep python
kronos  9100  95.0  ...  python process.py

$ kill -15 9100
$ kill -9 9100

5. Find Top CPU Consumers

$ ps aux --sort=-%cpu | head -10
# Top 10 CPU hogs

6. Find Top Memory Consumers

$ ps aux --sort=-%mem | head -10
# Top 10 memory hogs

7. Kill Process for Specific User

$ sudo pkill -9 -u alice firefox

8. Live Monitoring

$ top
# Press M → sort by memory
# Press P → sort by CPU
# Press u → filter by user
# Press k → kill a process

9. Find Processes by Terminal

$ ps -t pts/0
# All processes on terminal pts/0

10. Kill Interactive

# From top:
$ top
# Press k
# Enter PID: 5678
# Enter signal: 15
# ✅ Killed

Visual: Process Lifecycle

Creation ────→ Running ────→ Sleeping ────→ Terminated
                │   ▲              │              │
                │   │              │              │
                └───┘              └──────────────┘
              (CPU use)         (waiting for event)

Signals:
  SIGTERM (15)  →  "Please stop"     (can clean up)
  SIGKILL (9)   →  "STOP NOW!"       (cannot be caught)
  SIGHUP  (1)   →  "Reload config"   (for daemons)
  SIGSTOP (19)  →  "Pause"           (cannot be caught)
  SIGCONT (18)  →  "Resume"

Summary

CommandPurposeExample
ps auxView all processesps aux
ps -u userProcesses by userps -u kronos
topReal-time viewertop
kill -15 PIDGraceful terminatekill -15 5678
kill -9 PIDForce killkill -9 5678
pkill nameKill by namepkill firefox
kill -lList signalskill -l

Key takeaways:

  • Processes are running programs — each has a unique PID
  • ps gives a snapshot — use aux for detail
  • top shows real-time — press q to quit, M/P to sort
  • kill -15 is the graceful option — try this first
  • kill -9 is the nuclear option — last resort
  • pkill kills by name — convenient but be specific (-u, -f)
  • Signals are communication — SIGHUP reloads, SIGTERM terminates, SIGKILL forces
  • NEVER kill PID 1 — it’s the init system
  • Verify the PID before killing — a wrong PID can kill something important

Remember: The process viewer ps and top are your eyes into what’s running. kill and pkill are your hands for stopping it. The golden rule: always try SIGTERM (15) first — it gives the process a chance to save state and clean up. Only use SIGKILL (9) when the process is truly stuck. And never kill a process you don’t understand — especially not PID 1!


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!