Tuesday, January 31, 2012

Cron Job to restart Jetty Server

If you have an Java hosting on a virtual server, you normally have limited resources for that. That means you only have a certain amount of RAM. This leads to memory problems when deploying large applications. Restart the server by hand is annoying. Fortunately there are cron jobs. A cron job is a task in Linux which is to be executed at specific times automatically. The simplest way to start a cron job in Linux is to place a shell script below one of the following folders:

I intend to show a shell script for Jetty 8 server which is installed in /opt/jetty-distribution-8.1.0.RC2/ (path on my virtual server). The script checks the process id of the running Jetty server, kills it if it was found and restarts the server with the command "nohup java -jar start.jar".
#! /bin/sh

jettyPid='pgrep -f "java -jar start.jar"'

if [ "$jettyPid" != "" ]
kill -9 $jettyPid

cd $jettyHome
nohup java -jar start.jar

Alternative shorter variant:
#! /bin/sh

pgrep -f "java -jar start.jar" | xargs -i kill {}

cd /opt/jetty-distribution-8.1.0.RC2/
nohup java -jar start.jar


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.