A Keep-Alive Program You Can Run Anywhere

Here's an article from the current Linux Gazette that offers a Java program to stop your machine from timing out.

The following article is taken from the February issue of our on-line sister publication, Linux Gazette.

You are halfway through typing a new program into a remote machine connected over a dial-up line, and you get called to intervene in a fight between your partner's miniature poodle and the neighbour's ugly yellow Labrador. When you get back, your connection has timed-out.

Is this something that has happened to you? Or perhaps you had to drag your kids away from a particularly offensive episode of Jerry Springer, then found you had to stick around to make sure the kids didn't come back?

The traditional procedure for maintaining activity on your line during an interruption of the type outlined above was to use a "fortune" program in a small loop, so a random saying was written to your screen every half-minute. This could present some real problems if a person with fair hair looked at your screen and saw something like:

Q: How do you make a blonde's eyes light up?A: Shine a flashlight in her ear.

Of course, you could have used an -i or equivalent parameter to restrict fortune from generating inoffensive material. The more recent incarnations of the fortune program offer their users a more specific set of options.

For The Terminally Challenged

If you only use a browser to read your Hotmail messages, you probably won't want to open a terminal window so you can run a fortune program. If you are using an X11-compliant window manager, you could start a clock program with something like:

 xclock -digital -update 1 &

But that's not going to work on your your vintage Windows 95 machine unless you also happen to be running something like PC-Xware.

On the other hand, the KeepAlive.java program, presented below, is designed to work anywhere. It's written in Java 1.1, so even the jview virtual machine on a basic Microsoft machine can handle it. It doesn't rely on finding fortune, xclock or any other program on a remote machine. And you don't have to change anything when you connect via a different ISP.

Finding A Partner

But to use this, you have to send traffic somewhere, right? So how do you find a partner machine that will receive your traffic? If we were writing this program as a shell script, we might work out where our gateway was and ping it at appropriate intervals. That's not so easy to do in a Java program, which might run on any number of platforms. In any case, it would be nice if we could send traffic somewhere beyond the gateway machine.

In almost every sort of networking arrangement, the participating machines have knowledge of one or more name server addresses. So what we can do from our Java program is make periodic requests to those name servers. We need to ensure that any hosts whose addresses we request cannot be found locally in a hosts table. And we also need to ensure that the answers to our name server requests are not cached locally. If you take a look at the program below, you can see that the names of the hosts whose addresses we are requesting are generated by examining the clock-time in milliseconds at the time of each request. This approach results in names like A1040689223909, A1040689229448 and so on.

That's really all we need to do, but it's nice to be able to see something happening. So our program defines a MessageFrame class that displays two colored buttons in a GUI window. The colors of these are changed at each iteration. We also set the title on the GUI window and change it at each iteration; this way, we still can see something happening when the window is minimized. And we set up a listener to detect window-closing events and to perform a graceful shutdown.

Getting It Together

The script is available in Listing 1.

Listing 1. KeepAlive.java

You need to compile it with a command like:

 javac KeepAlive.java

This command generates three class files containing code that can be executed on a Java virtual machine. Therefore, you can copy those class files to a directory on another machine, then execute them with a command like:

 java KeepAlive

To use the Microsoft virtual machine on a Windows box, use:

 java KeepAlive
/* @(#) KeepAlive.java  Trivial keep-alive program. Tries at 5-second intervals
 *                      to find addresses for hosts with generated names. This
 *                      ensures that messages are sent to nameserver(s).
 *                      Copyright (c) 2002 Graham Jenkins <grahjenk@au1.ibm.com>
 *                      All rights reserved. Version 1.06, August 15, 2002.
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
public class KeepAlive {
  public static void main(String[] args) {
    MessageFrame f=new MessageFrame();  // Change button colours each iteration.
    int flag=0;                         // Also switch frame-title so we can see
    while ( true ) {                    // activity whilst iconified.
      f.statusMess(Color.red,Color.red); f.setTitle("==X==");
      try {InetAddress addr=InetAddress.getByName("A"+(new Date()).getTime());}
      catch (UnknownHostException ioe) {}
      if(flag==0) {f.statusMess(Color.yellow,Color.green); f.setTitle("1.06");}
      else {f.statusMess(Color.green,Color.yellow); f.setTitle("KeepAlive");}
      try {Thread.sleep(5000L);} catch (InterruptedException e) {}
class MessageFrame extends Frame implements ActionListener {
  private Button b1, b2;                // Displays two coloured buttons.
  public MessageFrame() {
    Panel p=new Panel(); p.setLayout(new FlowLayout());
    b1=new Button() ; b2=new Button(); p.add(b1); p.add(b2);
    this.add("South",p); this.setSize(150,50); this.show();
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) { System.exit(0); }
  public void statusMess(Color left, Color right) {
    b1.setBackground(left); b2.setBackground(right);
  public void actionPerformed(ActionEvent e) {}

If you have Java 1.1 or later and no requirement to use the Microsoft virtual machine, you can assemble the class files into a single jar file, then execute it using the -jar option, like this:

  echo "Main-Class: KeepAlive\015" >/tmp/MyManifest
  jar cmf /tmp/MyManifest /tmp/KeepAlive.jar *.class
  java -jar /tmp/KeepAlive.jar


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: A Keep-Alive Program You Can Run Anywhere

Anonymous's picture

It seems like poor internet citizenship to bother a name server with worthless requests.

Re: A Keep-Alive Program You Can Run Anywhere

Anonymous's picture

ping is too difficult to use?

Re: A Keep-Alive Program You Can Run Anywhere

Anonymous's picture

As it is a use anywhere app, it can be used on windows which doesn't have an inter packet timing option. So to not bombard a server with icmp requests its this or a ping application.

As my firewall blocks icmp I find this useful.

Re: A Keep-Alive Program You Can Run Anywhere

Anonymous's picture

Java can't generate ICMP packets, you must use a C dll using JNI but ... where is portability ???? ;-)

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Upcoming Webinar
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
11am CDT, April 29th
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot