L2 Support Engineer · Fintech · Week 5
Week 5 Day 1 Day 2
Week 5 · Day 1 & Day 2

Bash Basics &
Log Automation

Day 1 — write your first real scripts using variables, loops and cron. Day 2 — automate log monitoring so errors are detected automatically without you watching.

Day 1 — Bash Basics Day 2 — Log Automation VariablesLoops grepcron
Day 1 Bash Basics — Variables, Loops & Cron
01 The Simple Idea
Real-life Analogy

Imagine you check 5 servers every morning — same commands, same steps, every single day. That's like brushing your teeth the exact same way every morning. You don't think about it, you just do it.

A bash script is the automation of that habit. Write the steps once. Run one command. The computer does all 5 servers in 10 seconds while you drink your coffee.

02 Day 1 Scripts — Written from Scratch
script-1.sh

Variables + echo — Your First Script

Start Here
script-1.sh
#!/bin/bash
# Script 1 — Variables and echo
# Variables store values. $ reads them back.

NAME="Urwah"
ROLE="L2 Support Engineer"
DATE=$(date)

echo "Hello $NAME!"
echo "Role : $ROLE"
echo "Today : $DATE"
echo "Starting your shift..."
Expected output when you run it
Hello Urwah!
Role : L2 Support Engineer
Today : Thu Apr 3 09:00:00 PKT 2025
Starting your shift...
How to run: Save as script-1.sh → chmod +x script-1.sh → ./script-1.sh
script-2.sh

for Loop — Check Multiple Items Automatically

Loops
script-2.sh
#!/bin/bash
# Script 2 — for loop
# Instead of checking servers one by one,
# the loop does it for each item in the list.

echo "=== Checking Servers ==="

for SERVER in payment-server api-server db-server; do
  echo "Checking: $SERVER"
  echo " Disk : $(df -h / | awk 'NR==2{print $5}')"
  echo " Done."
done

echo "All servers checked."
Expected output
=== Checking Servers ===
Checking: payment-server
  Disk : 62%
  Done.
Checking: api-server
  Disk : 62%
  Done.
Checking: db-server
  Disk : 62%
  Done.
All servers checked.
How to run: chmod +x script-2.sh → ./script-2.sh
script-3.sh

if/else + Variables — Smart Disk Alert

if/else
script-3.sh
#!/bin/bash
# Script 3 — if/else decision making
# If disk is above limit: warn. If not: OK.

LIMIT=80
DISK=$(df / | awk 'NR==2{print $5}' | tr -d '%')

echo "Disk usage: $DISK%"

if [ $DISK -gt $LIMIT ]; then
  echo "WARNING: Disk above $LIMIT%! Act now."
else
  echo "OK: Disk is fine at $DISK%"
fi
Expected output — if disk is at 62%
Disk usage: 62%
OK: Disk is fine at 62%

— if disk is above 80% —
Disk usage: 87%
WARNING: Disk above 80%! Act now.
How to run: chmod +x script-3.sh → ./script-3.sh
03 Cron — Schedule Scripts to Run Automatically

What is Cron?

Cron is Linux's built-in scheduler. You give it a time + a command and it runs that command automatically every day/hour/minute — even when you're not there. To edit your schedule, run: crontab -e

⏱️ Cron Syntax — 5 fields then your command
Minute
*
0–59
Hour
*
0–23
Day/Month
*
1–31
Month
*
1–12
Day/Week
*
0=Sun
Command
/path/script.sh
full path
Cron LineWhen it runs
0 8 * * *Every day at 8:00 AM
*/30 * * * *Every 30 minutes
0 8 * * 1-5Weekdays only at 8 AM
0 0 * * 0Every Sunday midnight
Day 2 Log Automation — grep + Cron Monitoring
04 The Simple Idea
Real-life Analogy

Think of a smoke detector. It doesn't wait for you to smell smoke. It monitors the air 24/7 and screams the moment it detects something wrong — automatically.

Your log monitoring script is that smoke detector. It watches the log file, counts errors, and alerts you automatically — whether you're at your desk or asleep.

05 Day 2 Scripts — Written from Scratch
script-4-create-log.sh

Step 0 — Create a Sample Log File First

Run First
script-4-create-log.sh
#!/bin/bash
# Creates a sample payment log to test against
# Run this first so the other scripts have a file to read

cat > "$HOME/payment.log" << 'EOF'
[2024-03-15 09:00] [INFO ] Service started OK
[2024-03-15 09:01] [INFO ] TXN-001 received Amount: 5000
[2024-03-15 09:02] [INFO ] TXN-001 SUCCESS
[2024-03-15 09:03] [ERROR] DB_TIMEOUT connection failed
[2024-03-15 09:03] [ERROR] TXN-002 FAILED unable to write
[2024-03-15 09:04] [WARN ] Disk at 84%
[2024-03-15 09:05] [ERROR] DB_TIMEOUT connection failed
[2024-03-15 09:05] [ERROR] TXN-003 FAILED unable to write
[2024-03-15 09:06] [INFO ] TXN-004 SUCCESS
[2024-03-15 09:07] [ERROR] SOCKET_TIMEOUT gateway unreachable
EOF

echo "Log file created at $HOME/payment.log"
echo "Lines: $(wc -l < $HOME/payment.log)"
Expected output
Log file created at /home/kali/payment.log
Lines: 10
⚠️ Run this first before scripts 5 and 6. It creates the log file that they read.
script-5-error-check.sh

Auto-Detect Errors in Log File

Day 2 Lab
script-5-error-check.sh
#!/bin/bash
# Auto-detect errors in the payment log
# Counts errors and warns if too many found

LOG="$HOME/payment.log"
REPORT="$HOME/error-report.txt"
LIMIT=3

# Write header to report
echo "=== Error Check: $(date) ===" >> "$REPORT"

# Check if log file exists
if [ ! -f "$LOG" ]; then
  echo "ERROR: Log file not found at $LOG"
  exit 1
fi

# Count errors
ERR=$(grep -c "ERROR" "$LOG")
WARN=$(grep -c "WARN" "$LOG")

echo "Errors : $ERR" | tee -a "$REPORT"
echo "Warnings: $WARN" | tee -a "$REPORT"

# Alert if errors exceed the limit
if [ $ERR -gt $LIMIT ]; then
  echo "ALERT: $ERR errors found! Above limit of $LIMIT." | tee -a "$REPORT"
  echo "--- Top errors ---" | tee -a "$REPORT"
  grep "ERROR" "$LOG" | awk '{print $4}' | sort | uniq -c | sort -rn | tee -a "$REPORT"
else
  echo "OK: Error count ($ERR) is within limit." | tee -a "$REPORT"
fi

echo "Report saved: $REPORT"
Expected output
Errors : 5
Warnings: 1
ALERT: 5 errors found! Above limit of 3.
--- Top errors ---
      2 DB_TIMEOUT
      2 TXN-002
      1 SOCKET_TIMEOUT
Report saved: /home/kali/error-report.txt
How to run: chmod +x script-5-error-check.sh → ./script-5-error-check.sh
script-6-full-monitor.sh

Full Monitoring Script — Disk + Memory + Log Errors

⭐ Main Script
script-6-full-monitor.sh
#!/bin/bash
# Full monitoring script
# Checks disk, memory and log errors in one run
# Saves everything to a dated report file

LOG="$HOME/payment.log"
REPORT="$HOME/monitor-$(date +%Y-%m-%d).txt"
DISK_LIMIT=80
ERR_LIMIT=3
ISSUES=0

# Helper to print and save at the same time
log() { echo "$1"; echo "$1" >> "$REPORT"; }

log "========================================="
log " MONITOR REPORT — $(date)"
log "========================================="

# --- 1. Disk check ---
DISK=$(df / | awk 'NR==2{print $5}' | tr -d '%')
if [ $DISK -ge $DISK_LIMIT ]; then
  log "[DISK] WARNING: $DISK% — above $DISK_LIMIT%"
  ISSUES=$((ISSUES+1))
else
  log "[DISK] OK: $DISK%"
fi

# --- 2. Memory check ---
SWAP=$(free -m | awk 'NR==3{print $3}')
if [ $SWAP -gt 100 ]; then
  log "[MEM ] WARNING: Swap at ${SWAP}MB — RAM running low"
  ISSUES=$((ISSUES+1))
else
  log "[MEM ] OK: Swap at ${SWAP}MB"
fi

# --- 3. Log error check ---
if [ -f "$LOG" ]; then
  ERR=$(grep -c "ERROR" "$LOG")
  if [ $ERR -gt $ERR_LIMIT ]; then
    log "[LOG ] ALERT: $ERR errors found in log!"
    ISSUES=$((ISSUES+1))
  else
    log "[LOG ] OK: $ERR errors — within limit"
  fi
else
  log "[LOG ] NOTE: Log file not found"
fi

# --- Summary ---
log "-----------------------------------------"
[ $ISSUES -eq 0 ] && log "RESULT: All OK" || log "RESULT: $ISSUES issue(s) found — check above"
log "Report: $REPORT"
Expected output
=========================================
 MONITOR REPORT — Thu Apr 3 09:00:00 2025
=========================================
[DISK] OK: 62%
[MEM ] OK: Swap at 0MB
[LOG ] ALERT: 5 errors found in log!
-----------------------------------------
RESULT: 1 issue(s) found — check above
Report: /home/kali/monitor-2025-04-03.txt
How to run: chmod +x script-6-full-monitor.sh → ./script-6-full-monitor.sh
script-7-schedule.sh

Schedule Everything with Crontab — Run Last

Run Last
script-7-schedule.sh
#!/bin/bash
# Gives all scripts permission and schedules them
# Run this LAST after all scripts are in place

# Give permission to all scripts
chmod +x "$HOME"/script-*.sh
echo "Permissions set for all scripts."

# Schedule: full monitor every day at 8 AM
(crontab -l 2>/dev/null; echo "0 8 * * * $HOME/script-6-full-monitor.sh >> $HOME/cron.log 2>&1") | crontab -

# Schedule: error check every 30 minutes
(crontab -l 2>/dev/null; echo "*/30 * * * * $HOME/script-5-error-check.sh >> $HOME/errors.log 2>&1") | crontab -

echo "Cron jobs scheduled. Your schedule:"
crontab -l
Expected output
Permissions set for all scripts.
Cron jobs scheduled. Your schedule:
0 8 * * * /home/kali/script-6-full-monitor.sh >> /home/kali/cron.log 2>&1
*/30 * * * * /home/kali/script-5-error-check.sh >> /home/kali/errors.log 2>&1
How to run: chmod +x script-7-schedule.sh → ./script-7-schedule.sh
06 How to Run Everything — Step by Step
📋 Complete Run Order on Kali Linux
01

Copy all 7 scripts to your home folder — /home/kali/

02

Give all scripts permission at once

chmod +x ~/script-*.sh
03

Create the sample log file first — other scripts need this

./script-4-create-log.sh
04

Test each Day 1 script individually

./script-1.sh
./script-2.sh
./script-3.sh
05

Run the error checker — counts errors and saves report

./script-5-error-check.sh
06

Run the full monitor — main script, does everything

./script-6-full-monitor.sh
07

Schedule everything with crontab — run last

./script-7-schedule.sh
08

Confirm cron jobs are active

crontab -l
07 Real L2 Scenarios
01

It's 8 AM and you haven't even opened your laptop yet. The monitor script already ran at 8:00 via cron and saved today's report. You open it and instantly know the status — no manual checking needed.

02

Errors start appearing in the log at 2 AM. The error check script runs every 30 minutes via cron. By 2:30 AM it detects 5 errors and logs the alert to errors.log. You see it first thing in the morning — before any client calls.

03

You are asked to check multiple servers. Instead of checking one by one, your for loop script runs through all of them in sequence automatically. What used to take 10 minutes now takes 5 seconds.

✅ Week 5 · Day 1 & 2 Outcomes