2.6 KiB
2.6 KiB
Shell Scripting and Automation
Overview
This document tracks the implementation of shell scripts and automation tasks for our Ubuntu Server VM.
Shell Scripting Basics
1. First Script: System Health Check ✅
#!/bin/bash
# system-health.sh - Basic system health check script
# Create script
sudo nano /usr/local/bin/system-health.sh
Script contents:
#!/bin/bash
# System Health Check Script
echo "=== System Health Check ==="
echo "Date: $(date)"
echo
# System uptime
echo "=== System Uptime ==="
uptime
echo
# Memory usage
echo "=== Memory Usage ==="
free -h
echo
# Disk usage
echo "=== Disk Usage ==="
df -h /
echo
# CPU load
echo "=== CPU Load ==="
mpstat 1 1
echo
# Active system services
echo "=== System Services ==="
systemctl list-units --type=service --state=running
echo
# Recent system logs
echo "=== Recent System Logs ==="
tail -n 5 /var/log/syslog
Script Setup ✅
# Install required package
sudo apt install sysstat
# Make script executable
sudo chmod +x /usr/local/bin/system-health.sh
# Test script
sudo /usr/local/bin/system-health.sh
Test Results ✅
- Script executed successfully
- All system metrics collected:
- System uptime (4 users, load average visible)
- Memory usage (1.9GB total, 370MB used)
- Disk usage (75% used)
- CPU load (97.11% idle)
- Active services (15 services running)
- Recent system logs
Next Improvements
- Add Error Handling
#!/bin/bash
# Add error handling
set -e # Exit on error
exec 2> >(tee -a /var/log/system-health.error.log) # Log errors
# Function for error handling
check_command() {
if ! command -v $1 &> /dev/null; then
echo "Error: $1 is not installed" >&2
exit 1
fi
}
# Check required commands
check_command free
check_command df
check_command mpstat
check_command systemctl
- Add Logging
# Add timestamp and logging
LOG_FILE="/var/log/system-health.log"
exec 1> >(tee -a $LOG_FILE) # Log stdout
Would you like to:
- Implement these improvements
- Schedule regular execution
- Add more system checks
Automation Plan
-
System Health Monitoring
- Create basic health check script
- Add error checking
- Implement logging
- Schedule regular execution
-
Maintenance Tasks
- Create cleanup script
- Automate log analysis
- Monitor service status
-
Reporting
- Generate system reports
- Email notifications
- Performance tracking
Implementation Status
- Basic Script Creation
- Error Handling
- Logging Implementation
- Automated Scheduling