LUNAROPS · OPERATIONAL UPLINK 100% UPTIME 1,247d POSTS 893 JEFF.MOON@LUNAROPS.DEV UTC --:--:--

Linux Process Management

linuxprocessessignalsjobsadministration

Every running program is a process. Understanding how to view, control, and manage processes is essential for Linux administration.

Process Basics

What is a Process?

A process is a running instance of a program. Each process has:

  • PID: Process ID (unique identifier)
  • PPID: Parent Process ID
  • UID/GID: User/Group running the process
  • State: Running, sleeping, stopped, zombie
  • Priority: Scheduling priority

Process States

State Symbol Description
Running R Currently executing
Sleeping S Waiting for event
Uninterruptible Sleep D Waiting for I/O
Stopped T Suspended
Zombie Z Terminated but not cleaned up

Viewing Processes

ps - Process Status

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Current user's processes
ps

# All processes (standard syntax)
ps aux

# All processes (BSD syntax)
ps -ef

# Specific columns
ps -eo pid,ppid,user,%cpu,%mem,cmd

# Tree view
ps auxf
ps -ejH

# By name
ps aux | grep nginx
pgrep nginx

ps aux Columns

USER  PID  %CPU %MEM   VSZ   RSS TTY STAT START TIME COMMAND
root    1   0.0  0.1 168936 11204 ?   Ss   10:00 0:02 /sbin/init
Column Meaning
USER Owner
PID Process ID
%CPU CPU usage
%MEM Memory usage
VSZ Virtual memory (KB)
RSS Resident memory (KB)
TTY Terminal
STAT State
START Start time
TIME CPU time used
COMMAND Command

top - Real-Time Monitoring

1
top

Key commands in top:

  • q: Quit
  • h: Help
  • k: Kill process
  • r: Renice process
  • M: Sort by memory
  • P: Sort by CPU
  • c: Show full command
  • 1: Show individual CPUs

htop - Better top

1
2
3
4
5
# Install
sudo apt install htop

# Run
htop

Features:

  • Mouse support
  • Horizontal/vertical scrolling
  • Tree view (F5)
  • Search (F3)
  • Filter (F4)
  • Kill (F9)

Other Monitoring Tools

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Memory-focused
free -h

# I/O monitoring
iotop

# Network connections
netstat -tuln
ss -tuln

# Open files
lsof

# By process
lsof -p 1234
lsof -c nginx

Signals

Signals are notifications sent to processes.

Common Signals

Signal Number Default Action Description
SIGHUP 1 Terminate Hangup (reload config)
SIGINT 2 Terminate Interrupt (Ctrl+C)
SIGQUIT 3 Core dump Quit (Ctrl+)
SIGKILL 9 Terminate Force kill (cannot be caught)
SIGTERM 15 Terminate Graceful termination
SIGSTOP 19 Stop Pause (cannot be caught)
SIGCONT 18 Continue Resume stopped process

Sending Signals

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# By PID
kill 1234           # SIGTERM (default)
kill -15 1234       # SIGTERM explicitly
kill -9 1234        # SIGKILL (force)
kill -HUP 1234      # SIGHUP (reload)

# By name
killall nginx       # All processes named nginx
pkill nginx         # Pattern match

# Specific user
pkill -u alice

# Interactive
kill -l             # List all signals

Graceful vs Force Kill

1
2
3
4
5
# Always try graceful first
kill 1234

# Wait a few seconds, then force if needed
sleep 5 && kill -9 1234

Job Control

Jobs are processes started from the shell.

Background and Foreground

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Run in background
command &

# Move to background (Ctrl+Z then bg)
$ long_command
^Z
[1]+  Stopped                 long_command
$ bg
[1]+ long_command &

# Bring to foreground
fg
fg %1           # Job number 1

# List jobs
jobs
jobs -l         # With PIDs

Keeping Jobs Running After Logout

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# nohup - Immune to hangups
nohup command &
# Output goes to nohup.out

# disown - Remove from shell's job table
command &
disown

# tmux/screen - Terminal multiplexer
tmux
command
# Detach with Ctrl+B, D

Process Priority

Nice Value

Nice values range from -20 (highest priority) to 19 (lowest).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Start with lower priority
nice -n 10 command

# Start with higher priority (requires root)
sudo nice -n -10 command

# Change running process priority
renice 10 -p 1234           # Set nice to 10
sudo renice -10 -p 1234     # Higher priority

# View nice values
ps -eo pid,ni,cmd
top                         # NI column

/proc Filesystem

Virtual filesystem with process info:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Process 1234 information
ls /proc/1234/
cat /proc/1234/cmdline      # Command line
cat /proc/1234/status       # Detailed status
cat /proc/1234/environ      # Environment
ls -l /proc/1234/fd         # Open files
cat /proc/1234/limits       # Resource limits

# System-wide
cat /proc/cpuinfo           # CPU info
cat /proc/meminfo           # Memory info
cat /proc/loadavg           # Load average
cat /proc/uptime            # Uptime

Resource Limits

ulimit

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# View limits
ulimit -a

# Set max open files
ulimit -n 65535

# Set max processes
ulimit -u 4096

# Unlimited core dumps
ulimit -c unlimited

/etc/security/limits.conf

Persistent limits:

# /etc/security/limits.conf
alice    soft    nofile    65535
alice    hard    nofile    65535
@developers soft nproc     4096
*        soft    core      0

Practical Examples

Find Resource Hogs

1
2
3
4
5
6
7
8
9
# Top CPU consumers
ps aux --sort=-%cpu | head -10

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

# Using top
top -o %CPU
top -o %MEM

Kill Stuck Process

1
2
3
4
5
6
7
8
9
# Find process
ps aux | grep stuck_app
pgrep stuck_app

# Try graceful kill
kill 1234

# Force if needed
kill -9 1234

Monitor Specific Process

1
2
3
4
5
6
7
8
# Watch process
watch -n 1 'ps aux | grep nginx'

# Continuous top for one process
top -p 1234

# Using pidstat
pidstat -p 1234 1

Background Long Task

1
2
3
4
5
6
# Run backup in background, immune to hangup
nohup tar -czf /backup/data.tar.gz /data &
echo $!  # Print PID

# Check progress
tail -f nohup.out

Wait for Process

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Wait for specific PID
wait 1234

# In scripts
command1 &
pid1=$!
command2 &
pid2=$!
wait $pid1 $pid2
echo "Both done"

Quick Reference

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# View processes
ps aux                  # All processes
top / htop             # Real-time
pgrep name             # Find by name

# Kill processes
kill PID               # Graceful (SIGTERM)
kill -9 PID            # Force (SIGKILL)
killall name           # By name

# Job control
command &              # Background
Ctrl+Z                 # Suspend
bg                     # Resume in background
fg                     # Bring to foreground
jobs                   # List jobs

# Priority
nice -n 10 command     # Lower priority
renice 10 -p PID       # Change priority

Process management is daily work for system administrators. Master these tools to keep your systems running smoothly.

Comments