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

Guides

Server Health और Disk Alerts के लिए Bash Script कैसे लिखें

CPU, memory, disk जाँचने वाली Bash script — Capgemini/Wipro style junior task।

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

Quick answer

Hold this vertical picture:

  1. set -euo pipefail।
  2. /proc और df।
  3. Thresholds + exit।
  4. Cron absolute paths।
  5. Secrets नहीं।

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

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

Health script क्यों लिखें

मुंबई या Frankfurt के users रिपोर्ट करें तब देर। पाँच मिनट की Bash check पहली automation।

Why: Outage से पहले दबाव पहचानें। What: metrics, thresholds, report, optional alert। How: Amazon Linux/Ubuntu पर टुकड़े जोड़ें।

Concept 1: Safe skeleton

Why: set -euo pipefail गलतियाँ जल्दी दिखाता है। What: shebang, pipefail, chmod 700। How: हर ops script ऐसे शुरू।

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: Metrics इकट्ठा

Why: Vibes पर alert नहीं। What: /proc/stat, meminfo, df -P। How: Stable columns parse करें।

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 और exit

Why: Cron exit codes पढ़ता है। What: Report file; exit 0/2। How: Fake webhook 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

Why: न चलने वाली script बेकार। What: cron 5 मिनट; absolute paths। How: bash -n पहले।

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 shape

  1. shebang + set -euo pipefail
  2. disk percent
  3. =90 तो exit 2

  4. OK report
  5. cron >> log 2>&1

Hyderabad e-commerce

Disk 92%. Webhook alert।

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. set -euo pipefail?

Error/unset/pipe पर रुकता है।

Q2. Safe mode?

700।

Q3. MemAvailable कहाँ?

/proc/meminfo।

Q4. Cron absolute path क्यों?

PATH छोटा।

Q5. Fake webhook?

हाँ — secrets commit न करें।

Reading tracking

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

Skeleton → metrics → thresholds → report → cron। 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.