Detecting/controlling a process - java

I have a java application where I want to check to see if an application is running. If it is not running, I want to start it. If it is running, I want to kill it and then restart it.
Can someone tell me how to do this? I can start/stop the program easily enough, with the ProcessBuilder. But I cannot detect a process that is already running.
Thanks for your help!
John

Without the cooperation of the application (ideally have it listening on a network port), that may be impossible (your Java app might not have the rights to kill the app) and requires OS-specific code. On Linux, you'd uase the ps and kill commands.

Might sound silly, but you can create a file with a known name on application startup. Doesn't need to contain anything. To check if your application is running, check if that file exists.

Is the other application (the process you are monitoring) under your responsibility? If so, you can use a method I used in some high-availability system a few years back:
Open a connection to that other application and "ping" it. If the other application does not respond within a given timeout, it is either down or "hung", which is as bad (and something you can't detect through process monitoring.
Opening a connection can be done using sockets, or though more sophisticated protocols (SOAP?).
An alternative is to have the application send a "I'm alive" message every so often. If you haven't received it in some time - your application needs restarting.
In order to actually "kill" the other process, you can keep the Process instance you get from the exec() method, and destroy() it when you so choose.

Thanks for the replies. That was what I was afraid off. We are trying NOT to add more things to the application that I want to start up. We are basically trying to add a remote control web interface to a collection of applications.
The web server application that I am writing would basically start/stop 3 apps that all talk to each other to achieve a goal. If my web server starts and stops them, all is well. But if, for some reason they are already running when I try to start them bad things happen.
It is something I know I could handle with Visual Studio (C++/C#/etc). But this project has to be written in java due to a platform independence requirement.
Thanks for your help everyone!

Related

Notify a separate JVM to preform a task

So I am making a program that will only run one instance at a time and am doing so by using this solution
However now I would like to make it so that if the user trys to launch another instance it will consume that attempt and notify the current instance to show its gui.
Currently I am thinking about doing this by the use of a file. Upon the launching of a second instance, a file called show.stage will be created. When the other instance detects that file it will show its gui and delete the file.
I know this works but I was wondering if there was a more graceful way to do this.
Could I some how set a environment flag that the other instance could check for or maybe notify it via a socket listener, although those seem to be discouraged by others. I get the feeling creating the file will be the easiest and most robust way but I am open to any suggestions
This program will be running on normal windows.
If you don't want to use a lock file (which I think is a perfectly good solution), you can use sockets.
Have your application create a socket server and listen at some port in localhost. If it fails to listen, it would mean that someone else is listening to that port already - most likely, another instance of your app. You can even connect to that port and send messages to notify the primary instance that a second instance tried to be spawned.
The caveat is that if another app legitimately uses that port your app would never be able to run - but I find that very unlikely to happen.
There are many ways to go about this:
as you already figured, the file system can be used as communication channel between two jvms. But that only works for jvms running on the same server.
thus the already suggested socket solution enables you to (later) apply the same solution to a distributed environment. The downside is that you have to implement a protocol on a very low level.
in the past, people often turned to message bus solutions (think ActiveMQ for example)
in 2018, the other alternative would be to implement a simple restful API, using jaxrs and jersey for example.
As said: the effort you want to put into this depends on your requirements. How long will it be used? What are the odds that your solution will grow and has to scale to more than one server?!
try to use pidfile as lock and process single like kill 9 as communication tool

How to listen to Process Up or down events in Java?

I have a process running using Systemctl, configured it with Restart=always
so that even if process crashes then it will restart by itself without manual intervention. When that process restarts I want to take some action in my java code. I don't want poll the process. is there anyway to achieve this?
Thanks in advance.
systemd has a D-Bus API that you can use to receive event notifications when a unit changes state, including changes to its ActiveState property that tells you whether it's currently running. See this answer on Unix & Linux StackExchange for more information.
To use this API from a Java program, you'll need a D-Bus library for Java. It looks like there's a DBus-Java library, though it hasn't been updated in a long time. Alternatively, you might be able to have your Java program invoke the command-line dbus-send and/or dbus-monitor programs and read their output.

How to get information from a Java Process?

So, say I have multiple instances of a java program running, and I need to get information from each instance, ie if the program is performing its function correctly and if its not I need to get information to the process on what to do, I essentially need to open a tunnel. How would I do so? I hope this question isn't to vague.
I'm basically writing a manager, the manager loads a bunch of clients and I need a way to communicate between the manager and each client.
Have you looked into Java Management Extensions? It's built-in and made for monitoring java processes:
http://openjdk.java.net/groups/jmx/
if you need to look at custom info for your processes (some state of execution, or some special log info), you can create your own info-providers (aka "MBeans"):
https://blogs.oracle.com/WebLogicServer/entry/developing_custom_mbeans_to_ma
hope that helps

JNLP SingleInstanceService Use in Command Line JAR Application

I have a desktop Java application that is run from the command line, which takes in some arguments and performs some actions based on these arguments.
Currently, the application is instantiated periodically, performs its function and then exits.
The issue is that the users are unhappy with the amount of time it takes for the application to initialize. In order to work around this, I thought of simply toggling the visibility of the application when it is finished and setting up some kind of IDLE state.
I was trying to figure out a way to pass in new arguments next time the application needs to do work. I found out about SingleInstanceService and was wondering if it is possible to make this work with my application? It's unclear to me what I need to do so that the Single Instance Service runs on the client PC.
Alternatively, is there another solution for my communication problem? I would rather not depend on File I/O to trigger the application's logic.
Thanks.
AFAIK The JNLP API is available only if you launch your application using java web start (JWS) technology: read more here: http://java.com/en/download/faq/java_webstart.xml
If that is an option for you, oracle has some example of how to use the SingleInstanceService here
Implement and Register SingleInstanceListener. It will be invoked with the main-args when new instances of your application is launched.

How to run Apache-Solr server as background process?

I am using Apache-Solr for indexing and searching in my java application.
If want to perform any operation related to Apache-Solr then the solr server is must to be started.
Sometime the terminal in which Solr-server is running is closed accidentally then we have to start that server again, because we can not perform any Search/Index related operation.
I tried to run process in background by putting '&' at end of the command, but it does not working.
I do not want to that terminal is opened in which server is running. So is there any way to run the Apache-Solr-Server in background process?
Thanks in advance..
i think this link will be useful for you it talks about how to use screen if you are using linux and other stuff
start apache-solr-server in background
also you may start
Solr automatically by creating an init/LaunchDaemon script and let the
system handle running it.

Categories