Process Management
Process Management — Quick Reference
Section titled “Process Management — Quick Reference”ps — Snapshot of Running Processes
Section titled “ps — Snapshot of Running Processes”ps aux # all processes, full detailps aux | grep nginx # find a specific processps -ef # full-format listing (alt style)ps -p 1234 # info for a specific PID| Column | Meaning |
|---|---|
USER | Owner of the process |
PID | Process ID |
%CPU | CPU usage |
%MEM | Memory usage |
STAT | State (R=running, S=sleeping, Z=zombie) |
COMMAND | Command that started the process |
top / htop — Live Process Monitor
Section titled “top / htop — Live Process Monitor”top # built-in live monitorhtop # better UI (may need install)top key bindings:
| Key | Action |
|---|---|
k | Kill a process (enter PID) |
M | Sort by memory |
P | Sort by CPU |
q | Quit |
kill / killall — Terminate Processes
Section titled “kill / killall — Terminate Processes”kill 1234 # graceful stop (SIGTERM)kill -9 1234 # force kill (SIGKILL)kill -HUP 1234 # reload config (SIGHUP)
killall nginx # kill all processes named nginxkillall -9 node # force kill all node processesCommon signals:
| Signal | Number | Meaning |
|---|---|---|
SIGTERM | 15 | Graceful shutdown (default) |
SIGKILL | 9 | Force kill, cannot be caught |
SIGHUP | 1 | Reload config / restart |
SIGINT | 2 | Interrupt (same as Ctrl+C) |
Background & Foreground Jobs
Section titled “Background & Foreground Jobs”command & # run in backgroundCtrl+Z # suspend current processbg # resume suspended job in backgroundfg # bring background job to foregroundfg %2 # bring job #2 to foregroundjobs # list all background/suspended jobsnohup — Survive Terminal Close
Section titled “nohup — Survive Terminal Close”Runs a command that keeps running after you log out.
nohup ./script.sh & # run in background, immune to hangupnohup ./script.sh > out.log 2>&1 & # capture stdout + stderrOutput goes to nohup.out by default unless redirected.
Redirect & Pipes
Section titled “Redirect & Pipes”command > file.txt # stdout to file (overwrite)command >> file.txt # stdout to file (append)command 2> err.txt # stderr to filecommand > out.txt 2>&1 # stdout + stderr to same filecommand 2>/dev/null # discard stderrcommand1 | command2 # pipe stdout of cmd1 into cmd2pgrep / pkill — Find & Kill by Name
Section titled “pgrep / pkill — Find & Kill by Name”pgrep nginx # print PIDs of processes named nginxpgrep -l nginx # print PID + namepkill nginx # kill all processes named nginxpkill -9 node # force kill by namelsof — List Open Files / Ports
Section titled “lsof — List Open Files / Ports”lsof -i :8080 # what process is using port 8080lsof -i TCP # all TCP connectionslsof -p 1234 # files opened by PID 1234lsof -u username # files opened by usersystemctl — Manage Services (systemd)
Section titled “systemctl — Manage Services (systemd)”systemctl start nginx # start a servicesystemctl stop nginx # stop a servicesystemctl restart nginx # restartsystemctl reload nginx # reload config without restartsystemctl status nginx # check statussystemctl enable nginx # start on bootsystemctl disable nginx # don't start on bootsystemctl list-units # list all active units- Always try
kill(SIGTERM) beforekill -9— give the process a chance to clean up. pgrep -lis faster thanps aux | grepfor finding a PID.- Use
nohup+&for long-running jobs on remote servers. lsof -i :PORTis the fastest way to find what’s occupying a port.