Fork me on GitHub

Keep Apache Running

I recently encountered a problem on one of my servers with logrotate and the Apache web server. Logrotate is configured to run daily and help me keep all the log files at a manageable size. In the configuration, I have it set to rotate my Apache2 log files on a weekly basis.

The problem was, on one server, apache was not properly restarting after the log files were rotated. The script would run on Sunday morning, my logs would show that apache shutdown gracefully, but it would never come back up. The only log entry that gave me any indication of an issue was simply:
[notice] caught SIGWINCH, shutting down gracefully

In doing some research, I found that there were others with a similar issue, but no one had a solution. The best I could do was come up with a work-around. I created the following script and added it to my /etc/cron.hourly folder:
#!/bin/bash
if [ `pgrep apache2 -c` -le "0" ]; then /etc/init.d/apache2 start; fi

Now, every hour, this little script will count the number of apache processes that are running and, if it doesn't find any, start apache. 

Seems easy enough to me and, hopefully, this will help someone else out there, too.

Ciao!