1

I have a linux server in which my Java application(Jar) is runnning, I check this with command ps -ef | grep 'myapp.jar' and get 2 line output which states my app is Up and running.

Now we have one Web UI application. I want to show in the UI a status message like "My application is Up/Down" depending on my jar is running or stopped on the linux server.

How do i achieve this?

Farvardin
  • 5,336
  • 5
  • 33
  • 54

2 Answers2

0

You can use jcmd. Suppose the myapp.jar bootstrap class is com.myapp.Main then this fragment of code may help you:

    Runtime rt = Runtime.getRuntime();
    String[] commands = {"jcmd"};
    Process proc = rt.exec(commands);

    BufferedReader stdInput = new BufferedReader(new
            InputStreamReader(proc.getInputStream()));

    // read the output from the command
    String s = null;
    while ((s = stdInput.readLine()) != null) {
        String[] out = s.split("\\s");
        if("com.myapp.Main".equals(out[1])) {
            System.out.println("is running");
        }
    }
Radu Dumbrăveanu
  • 1,266
  • 1
  • 22
  • 32
0

Since you don't mention which application server you're running, I'm just going to go ahead and assume Apache Tomcat. If that is not the case, this answer will be absolutely useless.

If you have Tomcat Manager enabled or are willing to enable it, you can list currently running applications from it.

An example from a Tomcat server running just one application, "myWebApp", along with several default applications:

OK - Listed applications for virtual host localhost
/:running:0:ROOT
/myWebApp:running:0:myWebApp
/examples:running:0:examples
/host-manager:running:0:host-manager
/jsp:running:0:jsp
/manager:running:1:manager
/docs:running:0:docs

In order to use Tomcat Manager, you also need to enable at least one user with appropriate role in Tomcat. In Tomcat 7 the required role is manager-script, so you'd have to add following lines into $CATALINA_HOME/conf/tomcat-users.xml in order to add user called "status" with password "password" (please change these to something more reasonable):

 <role rolename="manager"/>
 <user username="status" password="password" roles="manager-script"/>

After you have enabled a user capable of querying Tomcat Manager, you can simply ask it to give you status of all running applications. Parsing text mode output is simple enough to give you a working example:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.StringBuilder;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TomcatWebappStatus {
    // This is cooked together from examples found in:
    //   http://stackoverflow.com/questions/4328711
    //   http://stackoverflow.com/questions/4883100
    private String getAppStatus(String url,
                                String app,
                                String user,
                                String pass) throws Exception {
        BufferedReader in;
        Matcher m;
        Pattern p = Pattern.compile("^/\\w+:(\\w+):(\\d+):" + app + "$");
        String auth = user + ":" + pass;
        String resp = "ERROR (application " + app + " not found)";
        URL website = new URL(url);
        URLConnection connection;


        connection = website.openConnection();
        if (user != null && user.length() > 0 &&
            pass != null && pass.length() > 0) {
          connection.setRequestProperty("Authorization",
              "Basic " + Base64.getEncoder().encodeToString(auth.          getBytes()));
        }
        in  = new BufferedReader( new InputStreamReader(
                                   connection.getInputStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            m = p.matcher(inputLine);
            if (m.find()) resp = m.group(1);
        }

        in.close();

        return resp.toString();
    }

    public static void main(String[] argv) throws Exception {
      TomcatWebappStatus t = new TomcatWebappStatus();
      String tomcatManagerURL = "http://localhost:8080/manager/text/list";
      String appName = argv[0];
      String tomcatManagerUsername = argv[1];
      String tomcatManagerPassword = argv[2];
      String s = t.getAppStatus(tomcatManagerURL,
                                appName,
                                tomcatManagerUsername,
                                tomcatManagerPassword);

      System.out.println("App \"" + appName + "\" is " + s);
    }
}

If you crank out bytecode from the example above, you should be able to run it locally against Tomcat 7 (at least, might work with other versions as well).

Should your your Web UI happen to be made with Java, you could plug that example into it. In case of javascript, query to /manager/text/list and parsing its output should do the trick.