Goal: You want to detect when your servlet container (Tomcat) is down (due to various reasons - mainly because of not enough memory resources), and bring it up again refreshed.
Idea: make a simple shell script to check periodically (using cron job) whether the container is present in the processes or the server OS was forced to cut it off because of not enough resources. If down (not present) bring it up again.
The script:
#!/bin/sh
STATUS=`ps -ef | grep /opt/mytomcat6 | grep -v "grep" | wc -l`
DATETIME=`date`
if [ $STATUS -lt 1 ]; then
/opt/mytomcat6/bin/shutdown.sh
sleep 1
/opt/mytomcat6/bin/startup.sh
echo "$DATETIME - Tomcat was down..Again. (Told you not to use
magnolia on 280 MB RAM server :)). Bringing it up again...">>/scripts/log
exit 0
else
exit 0
fi
/opt/mytomcat6 is the location on my target tomcat installation. You can use CATALINA_HOME or some other path identifier if you like.
Now place this script in a crontab:
Type in a console crontab -e , and place this line:
*/1 * * * * /scripts/checktomcat
This will run the above script (name as checktomcat into the /scripts/ directory) every minute checking if Tomcat is up. Running this script every 1 minute is not an overhead for the server (it's fast and its basically few comands piped together); the OS will barelly notice it :)
That's it. Place the scripts under /scritps/checktomcat, make it executable (chmod +x) and type the above crontab syntax. When Tomcat goes down, the script will pick the change within a minute (1 minute is the worst case) and will bring it up again, logging the occurence of the event using a timestamp.
Note: this script can be easilly adapted to check for anything else then Tomcat.
Hope you will find this useful.

