Restarting Apache on deploy


Often we have customers who have larger websites they deploy, or even multiple websites they deploy , and they need the apache cache cleaned or even the application restarted.
Often the reason people want this is so developers do not need to login with SSH, do not need root access, and things are just a little more automated.
There are several methods to do this, using incron or other things to act apon new files etc. One of the more simple methods is just having a cron check for a file, and when it finds that then it triggers the restart.

Here is a short script that would do that

#!/bin/bash

# file to touch which will restart apache
RF=/var/www/sitename/public_html/restart.txt

# log file that restarts will go into
LF=/var/log/apache2/error_log

if [ -e "${RF}" ]; then
/usr/sbin/service apache2 restart 2>/dev/null
/bin/echo "$(/bin/date -u): Had to invoke 'service apache2 restart'" >> $LF
/bin/rm -f "${RF}"
fi

You can customize this to do any service restart, or any application (including rails, tomcats etc). The idea is that if the script sees a file, it will trigger a command for you.
Once you have put the above into a file, chmod +x filename you need to then add it into cron. Running crontab -e will let you enter something like this

*/1 * * * * /root/cron-restartapache.sh

This will run the script every minute, feel free to run that more often, or even use incron to have it run when that file changes rather than checking every minute.

Things to note: When using crontabs always include the full path to the binary you are running. This one was written for Debian based distro , Centos/RHEL users will need to adapt that or email us to help.

,