Bash Script
Linux System Health Check
Welcome
This Bash script performs a comprehensive system health check and sends an email report with the results. It includes checks for system uptime, disk space usage, memory usage, load average, and the top 5 memory-consuming processes. It also detects if a system reboot is pending. The script supports a verbose mode that prints the results to the screen instead of sending an email.
For the script to successfully send email notifications, an SMTP (Simple Mail Transfer Protocol) server must be configured on the Linux host. The mail command used in the script relies on a properly configured mail transfer agent (MTA) such as sendmail, postfix, or exim.
This script is ideal for remote execution and cronjob deployment. For suggestions on how to deploy using a cron job, click here.
* Please be aware, the coding on this site is meant to guide. Testing these scripts in a non-production environment is strongly encouraged. I take no responsibility for use of any scripts on this site or on my github repositories.
Script:
#!/bin/bash
# In no event shall the author or copyright holder be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise..
# The software is provided 'as is', without warranty of any kind, express or implied
# This was developed by Jonathan Wilson, 03-AUG-2024
# More scripts available at https://www.jonathancw.com/projects/
#
# Email settings
EMAIL="[email protected]"
SUBJECT="System Health Check Report"
EMAIL_BODY=""
# Check if -v variable is set
VERBOSE=false
if [ "$1" = "-v" ]; then
VERBOSE=true
fi
# Function to append output to email body and optionally print to screen
append_to_body() {
local output="$1"
EMAIL_BODY="${EMAIL_BODY}${output}\n"
if [ "$VERBOSE" = true ]; then
printf "%s\n" "$output"
fi
}
# Run system health checks
append_to_body "Running system health checks..."
# Check uptime
UPTIME_OUTPUT=$(uptime -p)
append_to_body "Uptime: $UPTIME_OUTPUT"
# Check disk space
append_to_body "Disk Space Usage:"
DISK_SPACE_OUTPUT=$(df -h)
append_to_body "$DISK_SPACE_OUTPUT"
# Check memory usage
append_to_body "Memory Usage:"
MEMORY_USAGE_OUTPUT=$(free -m)
append_to_body "$MEMORY_USAGE_OUTPUT"
# Check load average
append_to_body "Load Average:"
LOAD_AVERAGE_OUTPUT=$(uptime)
append_to_body "$LOAD_AVERAGE_OUTPUT"
# Check top 5 memory-consuming processes
append_to_body "Top 5 Memory-consuming processes:"
TOP_PROCESSES_OUTPUT=$(ps aux --sort=-%mem | head -n 6)
append_to_body "$TOP_PROCESSES_OUTPUT"
# Check for pending reboot
REBOOT_PENDING=false
if [ -f /var/run/reboot-required ]; then
REBOOT_PENDING=true
append_to_body "Reboot is pending."
else
append_to_body "No reboot is pending."
fi
append_to_body "System health checks completed."
# Send email if not in verbose mode
if [ "$VERBOSE" != true ]; then
printf "%b" "$EMAIL_BODY" | mail -s "$SUBJECT" "$EMAIL"
fi
Cron Example
Step-by-Step Instructions
-
Open the Crontab Editor:
Open the crontab editor for the current user by running:
crontab -e
-
Add the Cron Job:
Add the following line to the crontab file to schedule the script to run every Friday at 4 AM:
0 4 * * 5 /opt/system_health_check.sh
Breakdown of the Cron Expression
0 4 * * 5
:
0
: Minute (0th minute of the hour)4
: Hour (4 AM)*
: Day of the month (every day of the month)*
: Month (every month)5
: Day of the week (Friday)
Full Crontab Entry Example
Here's how it would look within the crontab editor:
# Run the system health check script every Friday at 4 AM
0 4 * * 5 /opt/system_health_check.sh
Save and Exit
After adding the line, save and exit the crontab editor. The method to save and exit depends on the text editor you are using (e.g., :wq
for vi
or vim
, Ctrl+X
followed by Y
for nano
).
Confirmation
You can confirm that the cron job has been added by running:
crontab -l
This command will list all the cron jobs for the current user, and you should see your newly added job listed.