Forum Moderators: phranque
What I want to do is to check the log file every 5 mins and if there is evidence of a seg fault then apache should restart.
This should be simple to do. All I have to do is to grep the error.log for 'segmentation fault' and if it exists then run /etc/init.d/apache2 restart
1. How can I use grep to identify that the log entries exist?
2. How can I mark that I've already detected a seg fault?
For 1 I guess I could grep out to >check_file and if the file size of check_file is greater than 0 bytes then there must be an entry.
For 2 I guess I could just then grep -v to remove the seg fault entries.
Anyone have any ideas on how to do this? Is it easy enough with Linux scripting or should I use php or perl and run that on the command line via a cron job?
If linux scripting then how can I detect if a file size is greater than 0 bytes?
I know this is not solving the original problem but I keep having to restart apache twice a day (at random times) and I'd really like to automate it until the problem is fixed.
alternatively the unix command wc (word count) could be used to determine if your file has any contents
wc -l filename
returns the number of lines in filename, if non-zero restart apache
your best bet is to solve the problem rather than fix the symptom
use File::Tail;
=File::Tail->new("/some/log/file");
while (defined(=->read)) {
if (/segmentation fault/) {
# do something;
}
}
Or try this perl code (seen on bugtraq list 1999):
open(ACCESS,"/bin/tail -f -c +0 $logfile ¦") ¦¦ die "Can't open tail of log file";
while(<ACCESS>) {
if(/segmentatuion fault/) {
# do something
}
}
Both untested though ...
Regards,
R.
The character after "$logfile" and the 2 characters before "die" are vertical bars
#### log_check_faults
# Check Logfile for segmentation fault. If detected
# stop apache, remove entries from log, restart apache
#
cp /var/log/apache2/error_log /tmp/temp_log
grep -i segmentation /tmp/temp_log >/tmp/segmentation
if test -s /temp/segmentation
then
/etc/init.d/apache2 stop
grep -v -i segmentation /tmp/temp_log >/var/log/apache2/error_log
/etc/init.d/apache2 start
cat /tmp/segmentation ¦ mutt -s "Segmentation Fault Detected" log_alert@mysite.co.uk
fi
rm /tmp/segmentation
rm /tmp/temp_log