Ravindra BagaleCourses & study guides मराठी

Chapter 4: Linux Ops Refresh for DevOps

DevOps

4.5 Disk, process, ports — the triage trio

Why: the problem

Think of Flipkart during the Big Billion Days sale. One product page becomes slow. Not down, just slow. Users wait ten seconds and leave. The team must find out quickly: is the CPU full, is memory running out, is a disk full, or is one process eating everything? Four small commands answer these questions: top for CPU and memory, ps for the list of processes, df and du for disk, and ss for ports and connections. Experienced engineers run them in the same order every time, like a doctor who checks pulse, temperature and blood pressure before anything else. In this lesson you will make a server slow on purpose in three ways: a CPU hog, a memory hog and a log file that fills the disk. You will find each one using the triage trio, and stop it safely.

What we will build

You will use an Ubuntu 24.04 VM or the ops-lab container. You will install stress-ng, a tool that safely creates load. You will run a CPU hog and find it with top and ps. You will run a memory hog and watch it in free and top. You will grow a fake log file and find it with df, du and find. Finally you will check ports and open connections with ss and lsof, and save everything into a triage report file you can paste into a ticket.

What: every term in plain English

  • CPU — The part of the computer that does the work. 100% CPU means it has no free time.
  • Load average — How many tasks want the CPU, averaged over 1, 5 and 15 minutes. Compare it with the number of cores.
  • nproc — Prints how many CPU cores the server has.
  • Memory (RAM) — Fast storage for running programs. When it is full, Linux kills a process (the OOM killer).
  • top — A live screen of CPU, memory and the busiest processes. Press q to quit.
  • ps — Prints a list of processes once. Good for scripts and sorting.
  • df — Disk free. Free space per disk.
  • du — Disk usage. Size of folders and files.
  • ss — Socket statistics. Shows listening ports and connections.
  • Inode — A slot for one file. A disk can be "full" of tiny files even with free space. df -i shows it.
Start the lab Install tools Count cores Snapshot CPU and memory Top processes by CPU Disk check Ports check
CPU, memory, disk, ports: the triage trio top to bottom

How: step by step

  1. Start the lab

    docker run -it --rm --name trio ubuntu:24.04 bash
    

    What it does: A safe machine to overload.

    Output

    root@5c2d1e0a9b8f:/#
    
  2. Install tools

    apt-get update -qq && apt-get install -y -qq procps stress-ng iproute2 lsof
    

    What it does: Installs top, ps, stress-ng, ss and lsof.

    Output

    Setting up stress-ng ...
    
  3. Count cores

    nproc && uptime
    

    What it does: Load average must be read against the core count.

    Output

    2
     10:55:01 up 3 days,  load average: 0.10, 0.08, 0.05
    
  4. Snapshot CPU and memory

    top -b -n 1 | head -12
    

    What it does: -b is batch mode, so it prints once. Good for reports.

    Output

    %Cpu(s):  1.3 us,  0.7 sy, 98.0 id
    MiB Mem :   3911.0 total,   2100.4 free
    
  5. Top processes by CPU

    ps aux --sort=-%cpu | head -5
    

    What it does: The busiest processes first.

    Output

    USER PID %CPU %MEM COMMAND
    root   1  0.0  0.1 bash
    
  6. Disk check

    df -h / && df -i /
    

    What it does: Space and inode use.

    Output

    overlay 59G 21G 36G 37% /
    overlay 3.8M 210K 3.6M 6% /
    
  7. Ports check

    ss -tlnp
    

    What it does: Who listens where.

    Output

    State  Recv-Q Send-Q Local Address:Port
    

Example 1: Find a CPU hog

Stack. stress-ng, ps, top

Goal. Find a CPU hog.

Make the hog

stress-ng --cpu 2 --timeout 300s >/dev/null 2>&1 &
sleep 5
uptime
ps aux --sort=-%cpu | head -4
top -b -n 1 -o %CPU | sed -n 7,10p

Stop it safely

pgrep -a stress-ng
kill $(pgrep -o stress-ng)
sleep 2
uptime
ps aux --sort=-%cpu | head -3

What each line does

  • stress-ng --cpu 2 — Keeps two cores 100% busy. Our fake bad process.
  • & — Runs it in the background so we can type more commands.
  • load average 1.9 — About 2 tasks want CPU on a 2 core server. Fully busy.
  • ps aux --sort=-%cpu — Sorts by CPU, highest first. The hog is on top.
  • kill PID — Sends a polite stop signal (TERM). Use kill -9 only if it ignores this.
  • pgrep -o — Oldest matching process, the parent of the workers.

Green output

10:56:10 up 3 days,  load average: 1.92, 0.61, 0.24
USER  PID %CPU %MEM   VSZ  RSS COMMAND
root  412 99.6  0.1 57340 6012 stress-ng-cpu [run]
root  413 99.4  0.1 57340 6012 stress-ng-cpu [run]
  412 root  20 0 57340 6012 R 99.7 0.2 0:05.01 stress-ng-cpu
410 stress-ng --cpu 2 --timeout 300s
10:56:30 up 3 days,  load average: 1.40, 0.70, 0.29
USER  PID %CPU %MEM COMMAND
root    1  0.0  0.1 bash

Expected result. The hog was at the top of ps. One kill and the load starts to fall. In real life the hog is often a stuck Java thread or a runaway cron job.

Example 2: Find a memory hog before the OOM killer does

Stack. free, ps, dmesg

Goal. Find a memory hog before the OOM killer does.

Make it

free -m
stress-ng --vm 1 --vm-bytes 1500M --timeout 120s >/dev/null 2>&1 &
sleep 6
free -m
ps aux --sort=-%mem | head -3

Stop and check for OOM

kill $(pgrep -o stress-ng)
sleep 2
free -m
dmesg 2>/dev/null | grep -i "out of memory" | tail -2 || echo "no OOM lines (or no permission in container)"

What each line does

  • free -m — Memory in MB. Look at the available column, not free.
  • --vm-bytes 1500M — Uses 1.5 GB of RAM on purpose.
  • ps aux --sort=-%mem — Biggest memory user first. RSS is real memory used in KB.
  • dmesg | grep -i "out of memory" — Kernel messages. Shows when Linux killed a process to save memory.
  • available — Memory apps can still get. When it is near zero, trouble is close.

Green output

               total        used        free      shared  buff/cache   available
Mem:            3911        1210        1820          12         880        2450
Mem:            3911        2730         300          12         880         930
USER  PID %CPU %MEM    VSZ     RSS COMMAND
root  520 98.0 38.4 1593000 1536000 stress-ng-vm [run]
Mem:            3911        1220        1810          12         880        2440
no OOM lines (or no permission in container)

Expected result. Available memory dropped by 1.5 GB and ps named the process. On EC2, sudo dmesg shows OOM kills, a common cause of "my app restarted by itself".

Example 3: Find what is filling the disk

Stack. du, find, truncate

Goal. Find what is filling the disk.

Make a runaway log

mkdir -p /var/log/menu-api
for i in $(seq 1 200000); do echo "$(date) INFO order $i placed by user $RANDOM"; done > /var/log/menu-api/app.log
df -h /

Find it top down

du -xh / --max-depth=1 2>/dev/null | sort -h | tail -4
du -xh /var/log --max-depth=1 | sort -h | tail -3
find / -xdev -type f -size +10M -exec ls -lh {} \; 2>/dev/null | awk '{print $5, $9}' 

Free space without breaking the app

truncate -s 0 /var/log/menu-api/app.log
ls -lh /var/log/menu-api/app.log
df -h /

What each line does

  • du -xh / --max-depth=1 — Size of each top folder. -x stays on this disk.
  • sort -h | tail — The biggest come last.
  • find -size +10M — Every file larger than 10 MB, anywhere on this disk.
  • truncate -s 0 — Empties the file but keeps it open for the app. Safer than rm for a live log.
  • Why not rm? — If the app still has the file open, rm frees no space until the app restarts.

Green output

overlay  59G  21G  36G  37% /
12M  /usr
14M  /var
25M  /
13M  /var/log/menu-api
13M  /var/log
13M /var/log/menu-api/app.log
-rw-r--r-- 1 root root 0 Oct 25 11:01 /var/log/menu-api/app.log
overlay  59G  21G  36G  37% /

Expected result. You walked from the top folder to the exact file in three commands and freed it safely. On real servers also check /var/lib/docker and old journal files.

Example 4: Ports, connections and a triage report

Stack. ss, lsof, Bash

Goal. Ports, connections and a triage report.

Look at network use

python3 -m http.server 8000 >/dev/null 2>&1 &
sleep 1
ss -tlnp
ss -s
lsof -i :8000
ss -tn state established | wc -l

triage.sh

#!/usr/bin/env bash
OUT="triage-$(hostname)-$(date +%F-%H%M).txt"
{
  echo "== uptime and cores"; uptime; nproc
  echo "== memory"; free -m
  echo "== top cpu"; ps aux --sort=-%cpu | head -6
  echo "== top memory"; ps aux --sort=-%mem | head -6
  echo "== disk"; df -h -x tmpfs -x overlay 2>/dev/null || df -h /
  echo "== inodes"; df -i /
  echo "== listening ports"; ss -tlnp
} > "$OUT" 2>&1
echo "saved $OUT"

Run it

apt-get install -y -qq python3 >/dev/null
bash triage.sh
head -8 triage-*.txt

What each line does

  • ss -s — Summary of all sockets. Thousands of connections can mean an attack or a leak.
  • lsof -i :8000 — Which process has port 8000 open.
  • state established — Only live connections.
  • { ... } > "$OUT" — All output of the group goes into one file.
  • date +%F-%H%M — Date and time in the file name, so reports never overwrite each other.

Green output

State  Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0      5            0.0.0.0:8000      0.0.0.0:*    users:(("python3",pid=640,fd=3))
Total: 3
TCP:   1 (estab 0, closed 0, orphaned 0, timewait 0)
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 640 root 3u IPv4 81234 0t0 TCP *:8000 (LISTEN)
1
saved triage-5c2d1e0a9b8f-2026-10-25-1104.txt
== uptime and cores
 11:04:10 up 3 days,  load average: 0.21, 0.40, 0.30
2

Expected result. One command gives a full health picture you can attach to a ticket.

Verify: did it work?

  1. Load is back to normal

    uptime
    

    What it does: Load average below the core count.

    Output

    11:06:00 up 3 days,  load average: 0.12, 0.30, 0.28
    
  2. No hogs left

    pgrep stress-ng || echo none
    

    What it does: All test load is gone.

    Output

    none
    

When it is red 1

stress-ng: command not found

Cause. The tool is not installed.

Fix, step by step

  1. apt-get install -y stress-ng on Ubuntu.
  2. On Amazon Linux use dnf install -y stress-ng from EPEL, or use yes > /dev/null & as a simple CPU hog.

When it is red 2

No space left on device

Cause. The disk or the inodes are full.

Fix, step by step

  1. df -h for space and df -i for inodes.
  2. du and find to locate big files or folders with many files.
  3. truncate live logs, remove old rotated logs, docker system prune for images.

When it is red 3

df shows 100% but du adds up to much less

Cause. A deleted file is still open by a running process.

Fix, step by step

  1. lsof +L1 lists deleted files that are still open.
  2. Restart that process, or truncate it through /proc/PID/fd/N.
  3. Check df again.

Cleanup

  1. Stop background jobs

    kill %1 %2 2>/dev/null; jobs
    

    What it does: Stops any test jobs started in this shell.

    Output

    (no output)
    
  2. Leave the lab

    exit
    

    What it does: The container deletes itself because of --rm.

    Output

    exit
    

Ravindra Bagale's Tip

If the server is slow, check CPU, memory, disk, ports — in that order. Save a report, then fix. Keep that in mind!

Practice tasks

Easy Baseline report

Why: You need a normal picture to compare with.

What to do: Run triage.sh on an idle machine and keep the file.

How:
  1. bash triage.sh

Check: You have a saved baseline.

Medium Inode full

Why: Tiny files can fill a disk too.

What to do: Create 100000 empty files and watch df -i.

How:
  1. mkdir /tmp/many && cd /tmp/many && seq 100000 | xargs touch

Check: You see IUse% grow while df -h barely moves.

Hard Deleted but open

Why: A classic interview puzzle.

What to do: Open a big file with tail -f, delete it, and find it with lsof +L1.

How:
  1. Make a 500M file, tail -f it in background, rm it, df, lsof +L1.

Check: You freed the space by stopping tail.

Easy Say server triage with top, ps, df and ss in two lines

Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.

What to do: Write two short lines about server triage with top, ps, df and ss for a fresher who knows only basic Linux commands.

How:
  1. Write the problem it solves.
  2. Write the one command you use for it.
  3. Read it aloud to a friend.

Check: A fresher understands it without asking a question.

Easy Make a safe practice server

Why: Ops commands can stop services or fill disks. Practice on a machine you can throw away.

What to do: Start a throwaway Ubuntu machine and do this lesson there.

How:
  1. docker run -it --rm --name ops-lab ubuntu:24.04 bash
  2. Or launch a t3.micro Ubuntu 24.04 EC2 with port 22 open only to your IP.
  3. Delete it when you are done.

Check: All practice happens on the lab machine, not on a real server.

Medium Repeat it on Amazon Linux

Why: Companies use both Ubuntu and Amazon Linux. Package names and log files differ a little.

What to do: Do the main steps of this lesson again on Amazon Linux 2023.

How:
  1. docker run -it --rm amazonlinux:2023 bash
  2. Install tools with dnf instead of apt.
  3. Note every difference in your notes.

Check: You can list two differences between Ubuntu and Amazon Linux for this lesson.

Medium Break it on purpose

Why: You must know the red message by face. In a real outage there is no time to read slowly.

What to do: Cause one error from the "When it is red" part of this lesson and fix it.

How:
  1. Follow the error section.
  2. Copy the exact message into your notes.
  3. Write the fix in one line.

Check: You fixed it using only the steps, without searching the web.

Medium Write a one-page runbook

Why: At 2 a.m. nobody remembers the right command. A short page saves the on-call person.

What to do: Write a runbook page for this lesson.

How:
  1. Title, when to use, exact command, how to check, how to undo.

Check: A teammate can follow it alone.

Hard Explain it like an interview

Why: Interviewers want real stories, not definitions.

What to do: Record a 60 second answer about this lesson.

How:
  1. Say the problem, the command you ran, and one error you hit.

Check: Your answer has a real command and a real error message.

Hard Draw the flow top to bottom

Why: A drawing shows the order of checks and what happens on failure.

What to do: Draw this lesson as boxes from top to bottom.

How:
  1. Start at the user complaint, show each check as a box, end at the fix.

Check: Each box matches a real command you ran.

Homework: try at home

Try at home

At home, run triage.sh on your laptop or VM when it is idle, and again while stress-ng runs. Compare the two reports and mark every line that changed. Then write three lines: what was busy, how you knew, and what you would do.

Recap

  • top and ps for CPU and memory, sorted.
  • Load average compared with nproc.
  • df for free space, df -i for inodes, du and find for the cause.
  • ss and lsof for ports and connections.
  • Save it all in a triage report before you change anything.

FAQ

What is a good load average?

Below the number of cores. On 2 cores, 1.5 is busy but fine, 6 is overloaded.

Is high memory used always bad?

No. Linux uses free memory for cache. Look at available.

htop or top?

htop is nicer but not always installed. top is everywhere, so learn it first.

What is the OOM killer?

A kernel feature that kills a process when memory runs out, usually the biggest one.

How do I clean Docker disk use?

docker system df to see it, then docker system prune carefully.

Interview questions

  1. The server is slow. What do you check?

    uptime and nproc for load, top or ps sorted by CPU and memory, free -m, df -h and df -i, then ss for connections, then logs.

  2. df says 100% but du says 40%. Why?

    Deleted files still held open by a process. lsof +L1 finds them, and restarting the process frees the space.

  3. How do you find the biggest files on a server?

    du -xh / --max-depth=1 | sort -h, then go deeper, or find / -xdev -size +100M.

  4. What is load average?

    The average number of tasks running or waiting for CPU over 1, 5 and 15 minutes.

  5. How do you check which process uses port 3306?

    sudo ss -tlnp | grep 3306 or sudo lsof -i :3306.

Quick quiz

Q1. Which command shows processes sorted by CPU?

ps aux --sort=-%cpu

Q2. How do you read load average?

Compare it with the core count from nproc.

Q3. Which command finds big folders?

du -xh --max-depth=1 | sort -h

Q4. Why truncate instead of rm for a live log?

rm does not free space while the app keeps the file open.

Q5. Which command shows who uses a port?

ss -tlnp or lsof -i :port

Did you understand? Let us try it once more.

Ravi Sir's Cards for this lesson

मराठी cards