Below is a simple shell script that logs execution and sends notifications via email using the mail command [1][2]:
```shellscipt
#!/bin/bash
# Email notification settings
recipient="your@email.com" # Replace with the recipient's email address
sender="your@email.com" # Replace with the sender's email address
subject="Health Check Automation Report"
# Log file settings
log_file="/var/log/health_check.log"
exec 1> >(tee -a $log_file) # Redirect stdout to log file
exec 2> >(tee -a $log_file >&2) # Redirect stderr to log file
# Function to send email notifications
send_email_notification() {
message="$1"
echo -e "$message" | mail -s "$subject" -r "$sender" "$recipient"
}
# Health check automation steps
echo "Starting health check automation..."
date
# Step 1: Perform the health check
echo "Step 1: Performing health check..."
# Add your health check commands here
# Example: Check server status, database connections, etc.
if [ $? -ne 0 ]; then
error_message="Step 1: Health check failed. Please investigate."
send_email_notification "$error_message"
exit 1
fi
# Step 2: Additional health check steps
echo "Step 2: Additional health check steps..."
# Add more health check commands here
# All checks passed
echo "Health check completed successfully."
exit 0
```
Make sure to customize the following parts of the script:
1. Update the recipient and sender email addresses to your actual email addresses.
2. Customize the health check steps in the script. Add your own commands and checks in the appropriate sections.
3. Adjust the log_file variable to the path where you want to store the log file.
This script logs the execution of health check steps and sends an email notification in case any step fails. You can enhance the error handling and reporting as needed, such as sending notifications via different services or logging to other locations based on your requirements.