Process Management

Process Management

These commands help you monitor and control processes running on your Linux system.

ps

Displays information about active processes.

ps [options]

Options:

Examples:

# Show all processes
ps -e

# Show all processes with full details
ps -ef

# Show processes for current user
ps -u $(whoami)

# Show process tree
ps -ejH

# Show processes sorted by memory usage
ps -e --sort=-%mem

# Show processes sorted by CPU usage
ps -e --sort=-%cpu

# Custom format (PID, user, command, CPU, memory)
ps -eo pid,user,comm,%cpu,%mem --sort=-%cpu

top

Interactive dynamic real-time process viewer.

top [options]

Options:

Interactive commands (when top is running):

Examples:

# Run top with 3-second refresh
top -d 3

# Run top for specific user
top -u username

# Monitor specific PIDs
top -p 1234,5678

htop

An enhanced, interactive process viewer (more user-friendly than top).

htop [options]

Options:

Interactive commands (when htop is running):

Examples:

# Start htop
htop

# Monitor specific user's processes
htop -u username

# Start htop with processes sorted by memory usage
htop -s PERCENT_MEM

kill

Sends a signal to processes.

kill [options] pid...

Options:

Common signals:

Examples:

# Send SIGTERM (default) to a process
kill 1234

# Force kill a process
kill -9 1234

# List available signals
kill -l

# Send HUP signal (often used to reload configurations)
kill -s HUP 1234

killall

Kills processes by name.

killall [options] name...

Options:

Examples:

# Kill all processes with a specific name
killall firefox

# Force kill all processes
killall -9 firefox

# Kill processes owned by a specific user
killall -u username firefox

# Interactive kill with confirmation
killall -i firefox

pkill

Sends signal to processes based on name and other attributes.

pkill [options] pattern

Options:

Examples:

# Kill processes matching pattern
pkill firefox

# Kill processes matching full command line
pkill -f "firefox -private"

# Kill processes owned by specific user
pkill -u username firefox

# Send specific signal
pkill -HUP nginx

pgrep

Lists process IDs matching criteria.

pgrep [options] pattern

Options:

Examples:

# Find PIDs matching pattern
pgrep firefox

# Show process names along with PIDs
pgrep -l firefox

# Find processes matching full command line
pgrep -f "firefox -private"

# Find processes owned by specific user
pgrep -u username firefox

# List full command line
pgrep -a firefox

nice

Runs a command with modified scheduling priority.

nice [options] command [arguments]

Options:

Examples:

# Run command with lower priority (nice value 10)
nice tar -czf backup.tar.gz /home

# Specify nice value (higher number = lower priority)
nice -n 15 tar -czf backup.tar.gz /home

# Specify nice value (lower number = higher priority)
sudo nice -n -10 important_task

renice

Alters priority of running processes.

renice priority [options] identifier

Options:

Examples:

# Change priority of a process
sudo renice -n 10 -p 1234

# Change priority for all processes owned by a user
sudo renice -n 5 -u username

nohup

Runs a command immune to hangups, with output to a non-tty.

nohup command [arguments]

Examples:

# Run command that continues after logout
nohup long_running_script.sh &

# Specify output file (default is nohup.out)
nohup long_running_script.sh > output.log 2>&1 &

bg

Resumes suspended jobs in the background.

bg [job_spec...]

Examples:

# Resume the most recently suspended job in background
bg

# Resume specific job in background
bg %1

fg

Brings jobs to the foreground.

fg [job_spec...]

Examples:

# Bring most recently backgrounded job to foreground
fg

# Bring specific job to foreground
fg %1

jobs

Lists active jobs.

jobs [options]

Options:

Examples:

# List all jobs
jobs

# List jobs with PIDs
jobs -l

# List only running jobs
jobs -r

at

Executes commands at a specified time.

at time [date] [options]

Options:

Examples:

# Schedule a command for a specific time
at 10:00 AM
command1
command2
Ctrl-D

# Schedule a command for a future date
at 10:00 AM July 31

# Read commands from a file
at -f commands.txt 10:00 PM

atq

Lists pending jobs for the at command.

atq

atrm

Deletes jobs queued by the at command.

atrm job_number

Examples:

# Remove job number 23
atrm 23

crontab

Schedules periodic background jobs.

crontab [options]

Options:

Crontab format:

* * * * * command
| | | | |
| | | | +-- Day of week (0-7, where 0 and 7 are Sunday)
| | | +---- Month (1-12)
| | +------ Day of month (1-31)
| +-------- Hour (0-23)
+---------- Minute (0-59)

Examples:

# Edit your crontab
crontab -e

# List your crontab entries
crontab -l

# Remove your crontab
crontab -r

# Examples of crontab entries:
# Run at 5:00 AM every day
0 5 * * * /path/to/script.sh

# Run every hour
0 * * * * /path/to/script.sh

# Run every 15 minutes
*/15 * * * * /path/to/script.sh

# Run at 2:30 PM on the first of every month
30 14 1 * * /path/to/script.sh

# Run at midnight on weekdays (Monday-Friday)
0 0 * * 1-5 /path/to/script.sh

lsof

Lists open files and the processes that opened them.

lsof [options]

Options:

Examples:

# Show all open files
sudo lsof

# Show files opened by a specific process
sudo lsof -p 1234

# Show files opened by a specific user
sudo lsof -u username

# Show processes using a specific port
sudo lsof -i :80

# Show all network connections
sudo lsof -i

strace

Traces system calls and signals.

strace [options] command

Options:

Examples:

# Trace system calls made by a command
strace ls -l

# Attach to running process
sudo strace -p 1234

# Trace only specific system calls
strace -e open,read ls -l

# Trace with child processes
strace -f ./script.sh

# Generate summary report
strace -c ls -l

# Save trace to a file
strace -o trace.txt ls -l

time

Times a simple command or gives resource usage.

time [options] command [arguments...]

Options:

Examples:

# Time a command
time ls -R /

# Verbose timing information
/usr/bin/time -v gcc -o program program.c