Ravindra BagaleCourses & study guides मराठी

Chapter 4: Linux Ops Refresh for DevOps

DevOps

4.1 When the app is "down" — three checks

Why: the problem

Think of Swiggy at 8 p.m. on a Friday. Orders are pouring in. Suddenly the support team posts in Slack: "The app is down!" Five people jump on the server. One restarts nginx. One redeploys the app. One reboots the whole machine. Nobody knows what was wrong, so nobody knows what fixed it. Next Friday it happens again. This is the most common mistake in operations: changing things before looking. A calm engineer does three quick checks first. Is the process running? Is the port open and answering? Is the disk full? These three checks take less than one minute. They find the cause most of the time. In this lesson you will build a small practice server with nginx, break it in three different ways, and find each problem using only the three checks. After this lesson, "the app is down" will never again mean "restart everything".

What we will build

You will start an Ubuntu 24.04 lab machine and install nginx, a small web server. You will check that it answers on port 80 with curl. Then you will break it three ways: stop the process, move it to the wrong port, and fill the disk. Each time you will run the same three checks in the same order and see which one turns red. At the end you will put the three checks into a tiny script called three-checks.sh that prints OK or FAIL for each one.

What: every term in plain English

  • Process — A program that is running right now. nginx, java, node and python apps are all processes.
  • PID — Process ID. A number Linux gives every running process, like 1234.
  • Port — A numbered door on the server. Web traffic uses port 80 (HTTP) and 443 (HTTPS). An app listens on one port.
  • Listening — A process is waiting for new connections on a port. If nothing listens, connections are refused.
  • Disk full — The disk has no free space left. Apps cannot write logs or files, so they fail in strange ways.
  • curl — A command that calls a URL and prints the answer. It is how you test a website from the terminal.
  • systemctl — The command to start, stop and check services on modern Linux.
  • Triage — Sorting a problem quickly: find what is broken before you try to fix it.
Start a lab machine Install nginx and tools Start nginx Check 1: process Check 2: port Check 3: disk Final proof
App down: process, then port, then disk, top to bottom

How: step by step

  1. Start a lab machine

    docker run -it --name ops-lab -p 8080:80 ubuntu:24.04 bash
    

    What it does: Gives you a throwaway Ubuntu server. Port 8080 on your laptop goes to port 80 inside.

    Output

    root@3f2a9c1d7b44:/#
    
  2. Install nginx and tools

    apt-get update -qq && apt-get install -y -qq nginx curl procps iproute2
    

    What it does: Installs the web server plus ps, ss and curl.

    Output

    Setting up nginx (1.24.0-2ubuntu7) ...
    
  3. Start nginx

    nginx && echo started
    

    What it does: Starts the web server. In a container there is no systemd, so we start nginx directly.

    Output

    started
    
  4. Check 1: process

    pgrep -a nginx
    

    What it does: Lists nginx processes with their PID. Empty output means not running.

    Output

    1201 nginx: master process nginx
    1202 nginx: worker process
    
  5. Check 2: port

    ss -tlnp | grep :80
    

    What it does: Shows who listens on port 80.

    Output

    LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1201,fd=6))
    
  6. Check 3: disk

    df -h /
    

    What it does: Shows size, used and free space of the root disk.

    Output

    Filesystem Size Used Avail Use% Mounted on
    overlay     59G  21G   36G  37% /
    
  7. Final proof

    curl -sI http://localhost | head -1
    

    What it does: The server answers like a browser would see it.

    Output

    HTTP/1.1 200 OK
    

Example 1: Break 1: the process is dead

Stack. Ubuntu 24.04, nginx 1.24, curl

Goal. Break 1: the process is dead. Stop nginx and find it with the checks.

Break it

nginx -s stop
curl -sI http://localhost | head -1
echo "exit code: $?"

Run the three checks

pgrep -a nginx || echo "CHECK 1 FAIL: nginx not running"
ss -tlnp | grep :80 || echo "CHECK 2 FAIL: nothing on port 80"
df -h /

Fix and prove

nginx
pgrep -a nginx
curl -sI http://localhost | head -1

What each line does

  • nginx -s stop — Sends the stop signal to nginx. This is our fake crash.
  • curl -sI — -s is silent, -I asks only for headers. Fast and short.
  • head -1 — Shows only the first line, the status line.
  • pgrep -a nginx — Finds processes by name. -a shows the full command line.
  • || echo ... — Runs the echo only if the command before it failed. A cheap way to print FAIL.
  • nginx — Starts nginx again. On a real server you would use systemctl start nginx.

Green output

curl: (7) Failed to connect to localhost port 80 after 0 ms: Couldn't connect to server
CHECK 1 FAIL: nginx not running
CHECK 2 FAIL: nothing on port 80
overlay  59G  21G  36G  37% /
1301 nginx: master process nginx
1302 nginx: worker process
HTTP/1.1 200 OK

Expected result. Check 1 is red, so the cause is the process. Starting it fixes the site. You did not touch the code or reboot.

Example 2: Break 2: the process runs but on the wrong port

Stack. nginx, ss, sed

Goal. Break 2: the process runs but on the wrong port. Find it with check 2.

Break it

sed -i 's/listen 80 default_server;/listen 8081 default_server;/' /etc/nginx/sites-enabled/default
sed -i 's/listen \[::\]:80 default_server;/listen [::]:8081 default_server;/' /etc/nginx/sites-enabled/default
nginx -s reload
curl -sI http://localhost | head -1

Run the checks

pgrep -a nginx
ss -tlnp | grep nginx

Fix and prove

grep -n listen /etc/nginx/sites-enabled/default
sed -i 's/8081/80/' /etc/nginx/sites-enabled/default
nginx -t && nginx -s reload
curl -sI http://localhost | head -1

What each line does

  • sed -i — Edits the file in place. We change the listen port to 8081 to fake a bad config.
  • nginx -s reload — Tells nginx to read the config again without stopping.
  • ss -tlnp | grep nginx — Shows every port nginx is using. Here it shows 8081, not 80.
  • grep -n listen — Finds the listen lines and shows their line numbers.
  • nginx -t — Tests the config file. Always run it before reload.

Green output

curl: (7) Failed to connect to localhost port 80
1201 nginx: master process nginx
LISTEN 0 511 0.0.0.0:8081 0.0.0.0:* users:(("nginx",pid=1201,fd=6))
21: listen 8081 default_server;
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
HTTP/1.1 200 OK

Expected result. Check 1 was green but check 2 showed the wrong port. One config line fixed it. This is common after a careless deploy.

Example 3: Break 3: the disk is full

Stack. df, du, fallocate

Goal. Break 3: the disk is full. Find it with check 3 and clean safely.

Break it (lab only)

mkdir -p /var/lab-fill
fallocate -l 2G /var/lab-fill/big.bin
df -h /

Find what is big

du -xh /var --max-depth=1 2>/dev/null | sort -h | tail -5
ls -lh /var/lab-fill

Clean and prove

rm -f /var/lab-fill/big.bin
df -h /
curl -sI http://localhost | head -1

What each line does

  • fallocate -l 2G — Makes a 2 GB file instantly. We use it to eat disk space in the lab.
  • du -xh --max-depth=1 — Shows the size of each folder one level deep. -x stays on this disk.
  • sort -h — Sorts human sizes like 12K, 3M, 2G in the right order.
  • tail -5 — Shows the five biggest at the bottom.
  • rm -f — Deletes only the exact file we made. Never delete files you do not understand.

Green output

overlay  59G  23G  34G  41% /
4.0K /var/mail
12M  /var/cache
48M  /var/lib
2.0G /var/lab-fill
2.1G /var
-rw-r--r-- 1 root root 2.0G Oct 25 10:14 big.bin
overlay  59G  21G  36G  37% /
HTTP/1.1 200 OK

Expected result. You found the big folder in two commands and removed only the file you made. On a real server the big folder is usually /var/log or old Docker images.

Example 4: Put the three checks in one script

Stack. Bash, procps, iproute2

Goal. Put the three checks in one script.

three-checks.sh

#!/usr/bin/env bash
# Three checks before you change anything
NAME="${1:-nginx}"
PORT="${2:-80}"
if pgrep -x "$NAME" >/dev/null; then echo "1 process  OK   $NAME is running"; else echo "1 process  FAIL $NAME is not running"; fi
if ss -tln | grep -q ":$PORT "; then echo "2 port     OK   something listens on $PORT"; else echo "2 port     FAIL nothing on $PORT"; fi
USE=$(df --output=pcent / | tail -1 | tr -dc '0-9')
if [ "$USE" -lt 90 ]; then echo "3 disk     OK   root disk $USE% used"; else echo "3 disk     FAIL root disk $USE% used"; fi

Run it

bash three-checks.sh nginx 80
nginx -s stop
bash three-checks.sh nginx 80

What each line does

  • NAME="${1:-nginx}" — Takes the first argument, or nginx if none is given.
  • pgrep -x — Exact name match, so nginx does not match nginx-exporter.
  • ss -tln — Lists listening TCP ports with numbers, no names.
  • grep -q — Quiet grep. Only the yes or no answer matters.
  • df --output=pcent — Prints only the Use% column.
  • tr -dc 0-9 — Keeps only digits, so 37% becomes 37.
  • -lt 90 — Less than 90. Above 90% we call the disk a problem.

Green output

1 process  OK   nginx is running
2 port     OK   something listens on 80
3 disk     OK   root disk 37% used
1 process  FAIL nginx is not running
2 port     FAIL nothing on 80
3 disk     OK   root disk 37% used

Expected result. One command now tells you which of the three is broken. You will grow this script in later lessons.

Verify: did it work?

  1. Re-run the script after every fix

    bash three-checks.sh nginx 80
    

    What it does: All three lines must say OK before you close the incident.

    Output

    1 process  OK   nginx is running
    2 port     OK   something listens on 80
    3 disk     OK   root disk 37% used
    
  2. Test from outside the server

    curl -sI http://localhost:8080 | head -1   # run on your laptop
    

    What it does: Proves the path from a user to the server works, not only inside.

    Output

    HTTP/1.1 200 OK
    

When it is red 1

curl: (7) Failed to connect to localhost port 80: Connection refused

Cause. Nothing is listening on port 80. Either the process is dead or it listens on another port.

Fix, step by step

  1. Run pgrep -a nginx. If empty, start it.
  2. If it runs, run ss -tlnp and read the real port.
  3. Fix the listen line, run nginx -t, then reload.

When it is red 2

bash: ss: command not found

Cause. The small container image has no iproute2 package.

Fix, step by step

  1. apt-get install -y iproute2 on Ubuntu.
  2. dnf install -y iproute on Amazon Linux.
  3. As a backup, netstat -tlnp from net-tools does the same job.

When it is red 3

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

Cause. Another process already holds port 80. Maybe an old nginx or Apache.

Fix, step by step

  1. Run ss -tlnp | grep :80 to see the PID and name.
  2. Stop that service, for example systemctl stop apache2.
  3. Start nginx again and run the three checks.

Cleanup

  1. Leave the lab container

    exit
    

    What it does: Leaves the container shell. The container stops.

    Output

    exit
    
  2. Delete the lab container

    docker rm -f ops-lab
    

    What it does: Removes it fully. Next lesson starts fresh.

    Output

    ops-lab
    

Ravindra Bagale's Tip

If the site is down, do not panic. First process, then port, then disk. Restart comes after. Keep that in mind!

Practice tasks

Easy Run the three checks on a healthy server

Why: You must know what green looks like before you can spot red.

What to do: Run all three checks on your lab nginx and save the output.

How:
  1. pgrep -a nginx
  2. ss -tlnp | grep :80
  3. df -h /

Check: You have a saved green baseline.

Medium Find a port clash

Why: Port clashes are a top cause of failed deploys.

What to do: Start python3 -m http.server 80 before nginx and read the error.

How:
  1. apt-get install -y python3
  2. python3 -m http.server 80 & then nginx

Check: You read the bind() error and found the PID with ss.

Hard Add a fourth check

Why: Logs are the next step after the three checks.

What to do: Add a line to three-checks.sh that prints the last 3 lines of /var/log/nginx/error.log.

How:
  1. Use tail -3.
  2. Print a heading first.

Check: The script shows errors under the three checks.

Easy Say the three first checks 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 the three first checks 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, start the lab container and ask a friend to break nginx in one of the three ways without telling you which. Find the cause using only three-checks.sh and the commands in this lesson. Time yourself. Aim for under two minutes.

Recap

  • Look before you change anything.
  • Check process, then port, then disk.
  • pgrep, ss -tlnp and df -h are the three commands.
  • curl proves the site answers like a user sees it.
  • A small script makes the checks fast and the same every time.

FAQ

Do I need root for ss -tlnp?

Without root you see the ports but not the process names. Use sudo on a real server.

Why does my container have no systemctl?

Containers usually run one process, not systemd. On a real EC2 server you use systemctl.

What if all three checks are green but users still see errors?

Then look at logs next, in lesson 4.3, and at the network path like the load balancer and security group.

Is 90% disk really a problem?

Many teams alert at 80% and act at 90%. Logs can fill the last 10% very fast.

Can I use netstat instead of ss?

Yes. ss is newer and installed by default on most servers, so learn ss first.

Interview questions

  1. A user says the website is down. What do you do first?

    I do not restart anything first. I check the process with pgrep or systemctl status, the port with ss -tlnp and curl, and the disk with df -h. Then I read recent logs.

  2. How do you find which process uses port 8080?

    sudo ss -tlnp | grep :8080 shows the process name and PID. lsof -i :8080 also works.

  3. What is the difference between connection refused and timeout?

    Refused means the server answered that nothing listens on that port. Timeout means no answer at all, often a firewall or security group.

  4. Why is a full disk dangerous for apps?

    Apps cannot write logs, temp files or database data, so they crash or hang with confusing errors.

  5. Why should you avoid rebooting during an incident?

    A reboot hides the cause and the evidence. The problem usually comes back.

Quick quiz

Q1. What are the three checks, in order?

Process, port, disk.

Q2. Which command shows which process listens on a port?

ss -tlnp

Q3. What does curl error 7 usually mean?

Nothing is listening on that port, connection refused.

Q4. Why not reboot first?

You lose the evidence and do not learn the cause.

Q5. Which command shows free disk space?

df -h

Did you understand? Let us try it once more.

Ravi Sir's Cards for this lesson

मराठी cards