Log in
updated 11:29 AM UTC, May 4, 2016

Monit to start your Tomcat if it crashes

In the last article we came to know how to monitor website/server for failures, but it doesnt automatically starts the server program.

This task is better done using Monit program.

Monit is an open source tool that monitors and also has capability of managing the server programs.

Lets see how to install and configure monit to automagically starting your Tomcat server if it crashes.

Type this command to install:

sudo apt-get install monit

Check if monit daemon is up and running:

sudo monit status

make sure you dont get any error in this step. if you receive error , then make sure to uncomment the following lines in your /etc/monit/monitrc file.

set httpd port 2812
allow localhost       

Any changes to monitrc needs to be reloaded, use the following command:

sudo monit reload

Now try the 'sudo monit status' command , you should get similar to following output:

System 'ip-10-253-55-46.us-west-2.compute.internal'
  status                            Running
  monitoring status                 Monitored
  load average                      [2.05] [2.03] [2.05]
  cpu                               0.6%us 0.2%sy 99.0%wa
  memory usage                      109216 kB [17.8%]
  swap usage                        0 kB [0.0%]
  data collected                    Thu, 09 Jan 2014 06:32:24

 

Now add the tomcat to the monit list by adding following lines in the /etc/monit/monitrc file:

check process tomcat7 with pidfile /run/tomcat7.pid
start program = "/etc/init.d/tomcat7 start" with timeout 60 seconds
stop program = "/etc/init.d/tomcat7 stop"

 

now retype the command : 

sudo monit reload

now  start or stop all the services monitored by the monit by using:

sudo monit start all
OR
sudo monit stop all

That's it.

Now if the tomcat crashes , after 60 seconds it automagically gets restarted using the monit program.

 

 

Add a comment (1)

  • Written by Madhu V Rao
  • Category: General
  • Hits: 4229

Script to monitor your website for free

If you have hosted your website on Amazon Webservices (AWS) or any other cloud based hosting. You have to monitor your website for any errors , As its quite common for application server to go down from time to time due to various reasons. 

Its a good practice to keep a monitoring service which alerts you when website goes down (application server has died or instance is not accessible ,etc)

Here you can get a small script which does the job. you can set it up on your cloud instance from several locations to monitor for uptime from several locations.

Script monitors your website every minute (configurable) and on error notifies you with a mail. once recovered it will send out a mail about recovery. 

You need to have Curl installed on your ubuntu (if not below script will throw an error) and also need to have your mail details ready to send the mail on your behalf (SMPTS details)

Here it is:

#!/bin/bash
alert_sent=false
while true; do
	sleep 60 & pid=$!
	if curl -s --head  --request GET http://YOUR_WEBSITE.com | grep "200 OK" > /dev/null 
	then 
		if $alert_sent;
		then
			msg = "The HTTP server YOUR_WEBSITE.com has been recovered ";
        	        curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "FROM_YOUR_MAIL" --mail-rcpt "YOUR_MAIL_ID" --upload-file recovery.txt --user "YOUR_MAIL_ID:YOUR_PASSSSWORD" --insecure;
			alert_sent=false;
		else 
			echo "The HTTP server on YOUR_WEBSITE.com is up!" #> /dev/null;
		fi;
	else
		if $alert_sent;
		then
                        echo "The HTTP server on YOUR_WEBSITE.com is down!" #> /dev/null;
		else 
			msg="The HTTP server YOUR_WEBSITE.com is down ";
			curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "FROM_YOUR_MAIL" --mail-rcpt "YOUR_MAIL_ID" --upload-file mail.txt --user "YOUR_MAIL_ID:YOUR_PASSWORD" --insecure;
                        alert_sent=true;
		fi;
	fi;
	wait $pid;
done

It needs two more files which has  mail contents for error and recovery.

recovery.txt:

Hi,

Website YOURWEBSITE.com has been recovered.

Regards,
Self Monitoring

 

 mail.txt : this is the content for error notification:

Hi,

Website is down

Regards,
Self monitoring

 

Just copy the contents of the script to a webiste_monitor.sh and the contents of the text files to a text files.

keep all of them in a folder. change the website_monitor.sh to have executable permissions (chmod +x).

 

just run following in a terminal :

nohup ./website_monitor.sh &

 

that's it , if your website goes down, it will send out a mail.

 

 

 

Add a comment (2)

  • Written by Madhu V Rao
  • Category: General
  • Hits: 2206

How to give a meaningful name to your localhost in Ubuntu

Many of us at some point will install Apache server and start programming web based applications.

To test the apps, we use 127.0.0.1 or localhost as the web address  which doesnt give a personal touch to the development experience.

Its possible to change it to a meaningful or any name of your choice in Ubuntu easily.

Just edit /etc/hosts file by typing in command:

sudo gedit /etc/hosts

And change the line:

127.0.0.1	        localhost

to

127.0.0.1	       localhost          NameOfYourChoice.com

 

that's it, you can now type in webadress NameOfYourChoice.com in your web-browser to loopback the address.

This utility can also be used to test the website , when you are trying to host a new website and DNS names have no been propagated yet.

Just get the ipaddress of the hosting place and edit the file /etc/hosts file. You can test the website as if its already hosted.

 

Add a comment (1)

  • Written by Madhu V Rao
  • Category: General
  • Hits: 2801

Easy way to learn Cloud computing using Amazon EC2

Amazon has given a free tier of their EC2 (Elastic Compute Cloud).

You get 0.6G of Ram , around 40G of Hard-drive space , and a range of operating system to play around.

This is one time offer which can be utilised to know how to configure a cloud system.

My experience so far is very pleasant with detailed documentation of configs. My cloud is up and running in less than 10 mins.

I hope every developer utilizes this opportunity to learn about cloud computing.

 

Add a comment (1)

  • Written by Madhu V Rao
  • Category: General
  • Hits: 1854

How to highlight your code in Joomla articles

Code samples and examples are quite common in every programming related websites. Its quite important to format the code samples to appear as it is coded in the IDE (platform in which code is developed, most common example being Eclipse, Jbuilder, Netbeans , etc). Joomla provides some awesome tools for code formatting and here is one which I am using in this website for highlighting the code samples: CodeHighlighter

1) download the plugin and install it in Extension manager

2) open the plugin manager and publish the plugin

3) now open the code highlighter from the plugin list and enable the programming language samples which you will be providing in your website , this is quite important , it avoids unnecessary loading of the java scripts for the programming languages which you will not be using in your website.

4) There is another plugin codehightlighter button plugin which helps in adding the customized tags to enable the formatting. Download that and publish it from plugins list.

5) In your articles , just click on Insert code button and add your code.

That's it :)

Add a comment (1)

  • Written by Madhu V Rao
  • Category: General
  • Hits: 2905
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest