If you are new to Linux, or just forgot a lot of knowledge over years like me you might want to know how to create a Cron Job.
A Cron Job is a Task in Linux which is to be executed automatically at specific times. This can be for instance a time synchronisation script which needs to update your system time every 24 hours.
But, before we dive into cron jobs we need to define a task which should be executed.
If you are working on a virtual server you are granted limited resources of the host system. That means you only have a certain amount of RAM. This leads to memory allocation problems when deploying large applications, e.g. in a webapplication server like Jetty.
Imaging now an OOM (OutOfMemory Exception) occurs at 3 o'clock in the morning, you as the system administrator will probanly be sleeping and no one can access your website due to the server's downtime. But even you were awake and notices it, you have to manually restart the server by hand. Annoying right?
In short: Check the process id of your java process connected to Jetty and if it's down restart the server.
Here is the source code of the bash script jettycheck.sh:
jettyPid=`pgrep -f "java -jar start.jar"`
mydate=$(date +"%Y-%m-%d %k:%M:%S")
if [ "$jettyPid" == "" ]
echo "$mydate WARNING: Jetty is shutdown! Trying to restart it now!"
#exec start of jetty server
java -jar start.jar
With pgrep we can access all process ids running on the system. I.e. we want to make sure that we only get the process id of the jetty server returned by passing the command name as parameter. We have to do this because there may be multiple 'java' processes running on the machine and we have to distinguish them.
The next is really straightforward, checking if we found a process id, if not setting our jetty home, cding to it and starting jetty.
Note: we have to switch the current working dir to jetty home, otherwise it wont find the .xml configuration files from /etc/* located in jetty home. But there is also the possibility to pass these parameters to the start.jar directly by calling 'java -jar start.jar OPTIONS=Server,jsp /etc/jetty.xml ... ', so you could start jetty from any working directory.
You can save the shell script and add execute permission with
chmod 700 jettycheck.sh
Activating the Cron Job
Now that we got our script it is time to move on to the cron jobs part and let it automatically execute it. To do this we have to create a cron job:
crontab -ewe tell Linux to open an editor to edit our cron jobs according to the logged in user. Now that this may be our first cron job, a little explanation on the format:
# m h dom mon dow command
- m = minute
- h =hour
- dom=day of month
- dow=day of week
- command = the command which shoud be executed
0-59 * * * * /opt/java/server/jettycheck.sh >> /home/user/log/jettycheck.log #logs jetty errors and automatic restarts it
We also want to log the output from the script and therefore we have to create a .log file, e.g. in our users home log directory (log has to be created as well if it does not exist). If the nano editor was started to edit the crontab then we save with ctrl+o and exit with ctrl+x.
If we want to clear the log file, e.g. every 24 hours then we have to add following to the crontab:
0 0 * * * > /home/user/log/jettycheck.log #refreshes this log file every 24h
That's all you have to do to enable automatic monitoring of your jetty server! And a maximum of one minute downtime may be suitable for most personal websites.
Post a Comment