Linux: processes and resources
Finding and killing processes, CPU and memory, open ports and files, background jobs.
Processes
All processes
ps auxFind a process by name
pgrep -af <name>
Process tree
ps auxfOr pstree -p if it is installed.
Top 10 processes by memory
ps aux --sort=-%mem | head -11Top 10 processes by CPU
ps aux --sort=-%cpu | head -11Interactive monitor
htoptop is always available; htop is friendlier.
Stopping processes
Gracefully stop a process (SIGTERM)
kill <pid>
Force kill (SIGKILL)
kill -9 <pid>
The process can't clean up after itself — use only when SIGTERM doesn't help.
Kill all processes matching a name
pkill -f <name>
Kill whatever listens on a portadvanced
kill $(lsof -t -i :<port>)
Ports and files
Who listens on a port
lsof -i :<port>
All listening TCP ports with processes
sudo ss -tlnpFiles opened by a processadvanced
lsof -p <pid>
Deleted files still held open (disk not freed)advanced
sudo lsof +L1Memory and load
Memory usage
free -hUptime and load average
uptimeCPU, memory and IO every secondadvanced
vmstat 1Why a process was OOM-killedadvanced
sudo dmesg -T | grep -i -E 'killed process|out of memory'Background jobs
Run a command immune to logout
nohup <command> > output.log 2>&1 &
List jobs of the current shell
jobs -lBring a job to the foreground
fg %1Ctrl+Z suspends the current command, bg resumes it in the background.
Repeat a command every 2 seconds
watch -n 2 '<command>'