Linux Cheatsheet

Processes

Use this Linux reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Listing Processes

ps aux                      # all processes (BSD style)
ps -ef                      # all processes (UNIX style)
ps aux | grep nginx         # find by name
ps -u alice                 # processes by user
ps --forest                 # tree view
pstree                      # visual tree
pstree -p                   # with PIDs

top / htop

top                         # live process monitor
top -u alice                # filter by user
top -p 1234                 # monitor specific PID
htop                        # enhanced interactive (install separately)

top key bindings:

KeyAction
kkill process (prompts for PID, signal)
rrenice
Msort by memory
Psort by CPU
Tsort by time
ufilter by user
1toggle per-CPU view
qquit

Signals

kill 1234               # send SIGTERM (15) — graceful shutdown
kill -9 1234            # send SIGKILL — force kill (cannot be caught)
kill -HUP 1234          # SIGHUP (1) — reload config
kill -USR1 1234         # SIGUSR1 — app-specific
killall nginx           # kill by name (SIGTERM)
killall -9 nginx        # force kill by name
pkill nginx             # kill by pattern
pkill -u alice          # kill all processes of user
pgrep nginx             # list PIDs by name
pgrep -a nginx          # with full command line
SignalNumberDefault actionCommon use
SIGTERM15terminatepolite stop
SIGKILL9force killlast resort
SIGHUP1hangup/reloadreload config
SIGINT2interruptctrl+c
SIGSTOP19stopsuspend
SIGCONT18continueresume
SIGUSR1/210/12user-definedapp-specific

Background and Job Control

cmd &                   # run in background
ctrl+z                  # suspend foreground process
jobs                    # list jobs
jobs -l                 # with PIDs
fg                      # bring last job to foreground
fg %2                   # bring job 2 to foreground
bg                      # resume suspended job in background
bg %2                   # resume job 2 in background
disown %1               # detach job from shell (won't be killed on logout)
nohup cmd &             # run immune to HUP, redirect to nohup.out

Process Priority (nice / renice)

nice -n 10 cpu-task     # start with priority 10 (lower priority)
nice -n -5 cmd          # negative = higher priority (requires root)
renice -n 5 -p 1234     # change priority of running process
renice -n 5 -u alice    # renice all of user's processes

Niceness range: -20 (highest priority) to 19 (lowest). Default is 0.

System Resource Usage

top                         # live view
vmstat 1 5                  # virtual memory stats (every 1s, 5 times)
iostat -x 1                 # disk I/O stats
mpstat 1                    # per-CPU stats
free -h                     # memory usage
cat /proc/meminfo           # detailed memory
lsof                        # all open files
lsof -p 1234                # open files of PID
lsof -u alice               # open files by user
lsof -i :80                 # who's using port 80
fuser 80/tcp                # PID using port
fuser -k 80/tcp             # kill process using port

Detailed Process Info

cat /proc/1234/status       # process status
cat /proc/1234/cmdline      # full command line
cat /proc/1234/fd/          # open file descriptors
strace -p 1234              # trace syscalls of running process
strace cmd                  # trace syscalls of new command
ltrace cmd                  # trace library calls

nohup, screen, tmux

nohup ./long-task.sh &           # survive logout
nohup ./long-task.sh > out.log 2>&1 &

# screen
screen -S mysession         # start named session
screen -ls                  # list sessions
screen -r mysession         # reattach
# inside screen: ctrl+a d to detach, ctrl+a c new window

# tmux (preferred)
tmux new -s work            # new named session
tmux ls                     # list sessions
tmux attach -t work         # reattach
# inside tmux: ctrl+b d detach, ctrl+b c new window, ctrl+b % split vertical