Managing Processes and Jobs Using ps and kill

Learn how to view and manage Linux processes and background jobs using ps and kill. Step-by-step commands, examples, and safety tips for beginners and DevOps practitioners.

Managing Processes and Jobs Using ps and kill

Managing processes and jobs is a core skill for anyone who uses Unix-like systems. This guide explains the difference between processes and jobs, how to list and inspect them with ps, and how to control them safely with kill and job-control commands. Examples are provided so you can try commands in your terminal.

Understanding processes vs. jobs

A "process" is an instance of a running program managed by the kernel; it has a PID (process ID), state, memory usage, and other attributes. A "job" is a shell-level concept that represents one or more processes started from that shell (often in the foreground or background). Jobs are convenient for interactive work, while processes are what system-level tools (like ps) report.

Example: start a sleep process in the background and examine jobs and processes.

# Start a background job from your shell
sleep 300 &

# Check job list managed by the shell (bash/zsh)
jobs
# Output might be:
# [1]+  Running                 sleep 300 &

# Inspect the process at system level with ps
ps -o pid,user,stat,cmd -p $(pgrep -f "sleep 300")
# Example output:
#   PID USER     STAT CMD
# 23456 alice    S    sleep 300

Notes:

  • Jobs are referenced by job ID (e.g., %1) within the same shell.
  • Processes have numerical PIDs and are visible to system tools.

Listing processes with ps

The ps command is the standard tool to list processes. It supports both BSD-style and Unix (System V) options; commonly used forms are ps aux (BSD-style) and ps -ef (Unix-style). Use ps with -o to customize output columns.

Common examples:

# BSD-style, shows all processes with a user-oriented format
ps aux | head -n 6

# Unix-style, full-format listing
ps -ef | grep nginx

# Show specific fields for a PID
ps -p 23456 -o pid,ppid,user,%mem,%cpu,stat,etime,cmd
# Possible output:
#   PID  PPID USER  %MEM %CPU STAT     ELAPSED CMD
# 23456  1234 alice  0.1  0.0 S       00:03:24 sleep 300

Tips for filtering:

  • Use grep or pgrep to find specific processes: ps aux | grep sshd or pgrep -a sshd.
  • pgrep returns PIDs only; pgrep -a shows the command line.
  • When using ps with pipelines, be careful: some systems' ps outputs may differ slightly in ordering and fields.

Controlling processes with kill and signals

kill sends signals to processes. The default signal for kill is SIGTERM (15), which politely asks a process to terminate. SIGKILL (9) forces immediate termination and cannot be caught — use it only when SIGTERM fails.

Basic usage:

# Politely request process to exit (SIGTERM)
kill 23456

# Send a specific signal by name
kill -SIGTERM 23456
# Or by number
kill -15 23456

# Force kill (SIGKILL) — not recommended as first choice
kill -9 23456

Examples with pkill and killall:

# pkill terminates processes by name or pattern
pkill -f "python myscript.py"

# killall (on many Linux systems) kills by exact command name
killall nginx

Understanding signals:

  • SIGTERM (15) — polite shutdown, allows cleanup.
  • SIGINT (2) — like pressing Ctrl+C; interactive interrupt.
  • SIGKILL (9) — immediate termination, no cleanup.
  • SIGHUP (1) — hangup; many daemons reload configuration on SIGHUP.

Check signal handling:

# Start a script that traps SIGTERM
cat <<'EOF' > trap-demo.sh
#!/bin/bash
trap 'echo "SIGTERM received, exiting"; exit' SIGTERM
echo "PID $$ running. Send SIGTERM to test."
sleep 600
EOF
chmod +x trap-demo.sh
./trap-demo.sh &
# Then:
kill -15 <pid_of_trap-demo>

Always try SIGTERM first; reserve SIGKILL for processes that ignore polite signals.

Backgrounding, foregrounding, and job control

Interactive shells provide job control commands: jobs, bg, fg, and Ctrl+Z. These let you suspend, resume, and move jobs between foreground and background.

Examples:

# Start a long-running command in foreground
sleep 600

# Suspend it with Ctrl+Z (typed interactively)
# Output will show: [1]+  Stopped                 sleep 600

# List jobs
jobs
# Resume the job in the background
bg %1

# Or bring it back to foreground
fg %1

Notes:

  • jobs shows job ID, status (Running, Stopped), and command.
  • bg resumes a stopped job in the background; fg brings a job to the foreground.
  • You can use %n to refer to job number (e.g., %1) or %string to match the beginning of the command.

Detaching a job permanently:

# Send job to background and detach so it persists after logout
nohup long-task.sh &> long-task.log &
disown %1
  • nohup prevents the process from receiving SIGHUP on logout; disown removes the job from the shell's job table.

Commands table

Command Typical Options Description Example
ps aux / -ef / -o List processes; customize output ps aux \nps -ef \nps -p 1234 -o pid,ppid,user,cmd
pgrep -f -u user Search processes by name/pattern, returns PIDs pgrep -a sshd
pkill -f -u user Kill processes by name/pattern pkill -f "python myscript.py"
kill -SIGNAL PID / -9 PID Send signal to PID(s) kill -15 23456 \nkill -9 23456
killall (varies) Kill processes by command name killall nginx
jobs (no args) Show shell job list jobs
fg %job Bring job to foreground fg %1
bg %job Resume stopped job in background bg %1
nohup command & Run command immune to SIGHUP nohup myscript.sh &> out.log &
disown %job Remove job from shell control disown %1
top/htop (interactive) Real-time process viewer top \nhtop

This table summarizes the commands you'll use most often for inspecting and controlling processes and jobs.

Common Pitfalls

  • Using kill -9 as first resort: SIGKILL prevents cleanup (corrupted files, orphaned resources). Try SIGTERM first.
  • Confusing job IDs with PIDs: jobs uses %1, %2 etc.; kill requires numeric PIDs (use %? conversion like kill %1 won't work).
  • Assuming process names are unique: pkill/killall may match multiple processes; double-check with pgrep -a or ps before killing.

Next Steps

  • Practice safely: try starting simple sleep commands and use jobs, bg, fg, ps to observe behavior.
  • Learn process trees: use pstree or ps -o pid,ppid,cmd to understand parent-child relationships.
  • Explore monitoring: learn top/htop and set up simple scripts to restart services when they fail (systemd or supervisor are better for production).

👉 Explore more IT books and guides at dargslan.com.