Bash monitoring script that checks the disk usage of the root filesystem (/). If disk usage exceeds 80%, it sends an email alert to the system administrator.
Let’s break it down line by line.
1. Complete script
#!/bin/bash
# Cảnh báo khi ổ / dùng quá 80%
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$USAGE" -gt 80 ]; then
echo "Ổ đĩa đã dùng ${USAGE}%" | mail -s "Disk alert" admin@example.com
fi
2. Explain each command
Line 1: Define the Bash interpreter
#!/bin/bash
#!is called a shebang./bin/bashspecifies that the script should run using the Bash shell.- It tells Linux which interpreter to use when executing this script.
Line 2: Comment
# Cảnh báo khi ổ / dùng quá 80%
#indicates a comment.- Linux does not execute this line.
- It describes the purpose of the script.
Line 3: Get disk usage
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
This is the most important line. Let’s break it into three commands.
Step A: df /
Displays disk space information for the root filesystem.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 85G 15G 85% /
Step B: awk 'NR==2 {print $5}'
Extracts the fifth column from the second line.
NR==2: Select line number 2.print $5: Print the fifth column (85%).
Output:
85%
Step C: tr -d '%'
Removes the percentage symbol %.
tr: Translates or deletes characters.-d: Deletes specified characters.%: Character to remove.
Output:
85
Step D: Store the result in a variable
USAGE=$(...)
The $() syntax is called command substitution.
It executes the commands inside the parentheses and stores their output in the variable USAGE.
Result:
USAGE=85
Line 4: Check disk usage
if [ "$USAGE" -gt 80 ]; then
This checks whether disk usage exceeds 80%.
Important components:
| Component | Meaning |
|---|---|
if | Start a conditional statement |
[ ] | Test a condition |
$USAGE | Variable containing disk usage |
-gt | Greater than |
80 | Threshold |
then | Execute the following commands if true |
Bash comparison operators:
| Operator | Meaning |
|---|---|
-eq | Equal to |
-ne | Not equal to |
-gt | Greater than |
-lt | Less than |
-ge | Greater than or equal |
-le | Less than or equal |
For example:
if [ 85 -gt 80 ]; then
Result: TRUE.
Line 5: Send email alert
echo "Ổ đĩa đã dùng ${USAGE}%" | mail -s "Disk alert" admin@example.com
This sends an email notification.
Let’s break it down:
| Command | Explanation |
|---|---|
echo | Prints a message |
${USAGE} | Inserts the variable value |
% | Adds the percentage symbol |
\| | Pipes output to another command |
mail | Sends an email |
-s | Specifies the email subject |
"Disk alert" | Email subject |
admin@example.com | Recipient |
Example email:
Email notification
Alert
To: admin@example.com
Subject: Disk alert
Ổ đĩa đã dùng 85%
Line 6: End the condition
fi
ficloses theifstatement.- It is
ifspelled backward.
3. How does the script work?
Here is the execution flow:
Start Bash Script
Execute df /
Get root filesystem disk usage
Extract usage percentage
awk + tr → USAGE=85
Is USAGE greater than 80%?
NO
Do nothing
YES
Send email alert
End Script
4. Practical examples
| Disk usage | Condition | Result |
|---|---|---|
| 50% | 50 -gt 80 = FALSE | No email |
| 79% | 79 -gt 80 = FALSE | No email |
| 80% | 80 -gt 80 = FALSE | No email |
| 85% | 85 -gt 80 = TRUE | Send email |
| 95% | 95 -gt 80 = TRUE | Send email |
Notice that -gt 80 means strictly greater than 80%, not greater than or equal to 80%.
5. How to deploy this on Ubuntu Server
Step 1: Create the script
sudo nano /usr/local/bin/disk-alert.sh
Paste the script and save it.
Step 2: Make it executable
sudo chmod +x /usr/local/bin/disk-alert.sh
Step 3: Install mail utility
sudo apt update
sudo apt install mailutils -y
Note: You may also need to configure an SMTP service or relay for outgoing email.
Step 4: Test the script
sudo /usr/local/bin/disk-alert.sh
Step 5: Schedule it using Cron
sudo crontab -e
Add:
*/5 * * * * /usr/local/bin/disk-alert.sh
This runs the script every 5 minutes.
6. Recommended improvements for production
For a SysAdmin environment, I recommend three improvements:
- Monitor multiple mount points:
/,/var,/home,/data. - Avoid repeated email alerts: Send an alert only when usage crosses the threshold or implement alert cooldown.
- Add logging: Record disk usage and alert events in
/var/log/disk-alert.log.
My recommendation: Use this script as a beginner Linux Shell Scripting lab. It teaches five important SysAdmin concepts:
- Variables
- Command substitution
- Linux pipelines
- Text processing (
awk,tr) - Conditional statements (
if,-gt)
You can extend it into a more advanced disk monitoring solution integrated with Zabbix or Grafana.