Heartbeat service setup on Linux

In this blog I’m going to tell you how to setup heartbeat service on your machine to keep track if it up and running or for how long it has been down. To see if your machine is running or how long time since it made its last beat you can go to http://services.bech.in/heartbeat/. In order to add your server to the list please contact me and I will provide you with an ID.

For this to work the server needs the Java Runtime Enviroment (JRE) or some sort that being Oracle JRE or OpenJDK. You are going to need two files: script.sh and Beat.class. You can download them into using the following, it is important that you place them into the correct folder so the first change directory need to be right or it will not work!

cd ~/
wget http://services.bech.in/heartbeat/script.sh
wget http://services.bech.in/heartbeat/Beat.class

Next up we need to create the log-file there errors are write if any happen. To create the log-file use the touch-command:

touch heartbeat-log.txt

Now there are to things we left we need to do, first we need to edit the script to use your machine’s ID, so open the script.sh-file with you favorite text-editor and change the #Your ID# to the ID I have provided for you. If the ID is abc123 the line in the script.sh-file should look like this:

java Beat abc123

The finally thing we need to do is to add a cron job to the crontab of Linux, to do this simply open the crontab with the following command:

crontab -e

If it is the first time you use it will ask to select a text editor. Now add the following line to end of the file on a new line:

*/1 * * * * ~/script.sh | tee -a ~/heartbeat-log.txt

Save and close the file and you should get a response from cron like:

crontab: installing new crontab

Now go to http://services.bech.in/heartbeat/ to see if your machine is able to make a ping else consult the log-file. The page automatically updates every 10 seconds and the beat is made every minute with the above cron job.

Leave a Reply