This guide provides steps to monitor Apache error logs on a Linux system and receive email notifications when the logs contain the word “error.”
Locate the Apache error log file on your system. Update the ErrorLog
directive in your Apache configuration file to the desired log file path. For example:
/var/log/apache2/error.log
Ensure that the mailx utility is installed on your system. You can install it using your package manager.
Create a monitoring script, e.g., monitor.sh, with the following content:
#!/bin/bash
if grep -q "error" /var/log/apache2/error.log; then
echo "Apache error detected in error log." | mail -s "Apache Error Notification" user1@gmail.com
fi
Make the script executable:
chmod +x monitor.sh
Edit the crontab for the current user:
crontab -e
Add an entry to run your monitoring script at the desired interval. For example, to check every minute:
* * * * * /path/to/monitor.sh