Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

How to Write a Bash Script for Server Health and Disk Alerts

Write a Bash health script that checks CPU, memory and disk, prints a report, and can alert — the junior task Capgemini and Wipro-style teams still assign.

Friends, a health script is your first automation badge. Chala mitrano — alert only when thresholds break.

Quick answer

Hold this vertical picture:

  1. Safe skeleton with set -euo pipefail.
  2. Collect from /proc and df.
  3. Thresholds + exit codes.
  4. Cron with absolute paths.
  5. No real secrets in files.

Language: English · मराठी · हिंदी

Collect metricsCPU mem disk Compare thresholdse.g. disk 90% Write reportfile or stdout Optional alertmail or webhook
Health script flow

Why write a health script

Waiting for users in Mumbai or Frankfurt to report a full disk is too late. A five-minute Bash check is the first DevOps automation many teams trust.

Why: Catch pressure before outage. What: Bash script: metrics, thresholds, report, optional alert. How: Build pieces below on Amazon Linux or Ubuntu.

Concept 1: Safe script skeleton

Why: Broken scripts with set +u catch mistakes early. What: shebang, set -euo pipefail, chmod 700. How: Start every ops script this way.

Example 1: Shebang and safe mode

Type this:

printf '%s\n' '#!/usr/bin/env bash' 'set -euo pipefail' > /tmp/health.sh
head -2 /tmp/health.sh

You will see:

#!/usr/bin/env bash
set -euo pipefail

What it means: set -euo pipefail stops on errors and unset vars.

Example 2: Disk percent function

Type this:

python3 -c "u=91; print('ALERT' if u>=90 else 'ok')"

You will see:

ALERT

What it means: Threshold logic before any webhook.

Example 3: chmod private

Type this:

chmod 700 /tmp/health.sh
stat -c %a /tmp/health.sh 2>/dev/null || stat -f %A /tmp/health.sh

You will see:

700

What it means: Never world-writable scripts.

Concept 2: Collect CPU memory disk

Why: You cannot alert on vibes. What: /proc/stat, /proc/meminfo, df -P. How: Parse stable columns.

Example 4: CPU from /proc

Type this:

grep '^cpu ' /proc/stat | awk '{print \$2}'

You will see:

... idle ticks ...

What it means: /proc/stat feeds CPU math on Linux.

Example 5: Memory available

Type this:

awk '/MemAvailable/ {print \$2}' /proc/meminfo

You will see:

2048123

What it means: Values are kB. Convert for humans.

Example 6: Disk use awk

Type this:

df -P / | awk 'NR==2 {print \$5}'

You will see:

39%

What it means: Use df -P for stable columns.

Concept 3: Report and exit codes

Why: Cron and monitoring read exit codes. What: Write a report file; exit 0/2. How: Dry-run webhooks with fake URLs.

Example 7: Report file

Type this:

printf '%s\n' 'host=lab' 'disk=39%' 'mem_ok=yes' > /tmp/health-report.txt
cat /tmp/health-report.txt

You will see:

host=lab
disk=39%
mem_ok=yes

What it means: Reports beat silent scripts.

Example 8: Exit codes

Type this:

python3 -c "import sys; sys.exit(2)" ; echo exit=\$?

You will see:

exit=2

What it means: Non-zero exits drive cron alerts.

Example 9: Webhook dry-run

Type this:

printf '%s\n' 'POST https://hooks.example.invalid/alert' 'body={text: disk 91%}'

You will see:

POST https://hooks.example.invalid/alert
body={text: disk 91%}

What it means: Use a fake URL in labs; never commit real secrets.

Concept 4: Schedule and harden

Why: A script that never runs helps nobody. What: cron every 5 minutes; absolute paths. How: Log redirects; bash -n first.

Example 10: Cron line draft

Type this:

printf '%s\n' '*/5 * * * * /home/ec2-user/bin/health.sh >> /var/log/health.log 2>&1'

You will see:

*/5 * * * * /home/ec2-user/bin/health.sh >> /var/log/health.log 2>&1

What it means: Redirect stdout and stderr.

Example 11: PATH reminder

Type this:

printf '%s\n' 'Use absolute paths in cron' '/usr/bin/df not df'

You will see:

Use absolute paths in cron
/usr/bin/df not df

What it means: Cron has a tiny PATH.

Example 12: Test once

Type this:

bash -n /tmp/health.sh && echo SYNTAX_OK

You will see:

SYNTAX_OK

What it means: bash -n checks syntax without running.

Minimal script shape

  1. shebang + set -euo pipefail
  2. read disk percent
  3. if >= 90 then exit 2 and message
  4. else write OK report
  5. cron with >> log 2>&1

Suppose: Hyderabad e-commerce box

Disk hits 92 percent before Big Billion days. Script pages Slack-style webhook.

Suppose: Berlin SaaS VM

Memory Available drops. Same script pattern, different threshold.

Ravindra Bagale's Tip

At Capgemini and Wipro accounts, juniors are asked to script disk alerts before fancy tools. Keep scripts private (mode 700) and never store real webhook secrets in git.

Check yourself (quiz)

Q1. Why set -euo pipefail?

Fail on error, unset vars and pipe failures.

Q2. Safe file mode for scripts?

700 — owner only.

Q3. Where is MemAvailable?

/proc/meminfo on Linux.

Q4. Why absolute paths in cron?

Cron PATH is minimal.

Q5. Fake webhook in labs?

Yes — never commit real secrets.

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

Skeleton → metrics → thresholds → report → cron. Mode 700. No secrets. Did you understand? Let us try it once more.

Frequently asked questions

Why set -euo pipefail?

Fail on error, unset variables and pipe failures.

Safe mode for scripts?

700 — owner only.

Where is MemAvailable?

/proc/meminfo on Linux.

Why absolute paths in cron?

Cron has a minimal PATH.

Should labs use real webhooks?

No — use fake URLs; never commit secrets.

What exit code for alert?

Non-zero such as 2 for threshold breach.