I am running a java app that manages other java apps through starting them in different screen sessions. My problem is if one of these managed apps is not responding i'd like to kill its java process through my managing app.
The managing app starts other apps using screen -dmS appname java -jar path
The first thing i tried was to make my managing app run screen -S name -X quit but most of the time this only eliminates the screen session and i get stuck with a running java app that i have no access to.
The second thing i tried to research is to kill the java process itself which will in return terminate the screen but my problem is how can i get the PID of the java application?
ps -A is not helpful because it does not give any clue of the specific java application i want to kill among all others.
I need the PID to be available to my managing application or any other way that gives me the ability to terminate the java process running inside a screen.
However the best thing to solve my problem would be to be able to name the java app process.
I appreciate any help.
I solved the problem by passing an argument when i start any managed app like so:
screen -dmS helperApp java -jar path helperApp
This allowed me to identify the PID of the process among all others by doing
jps -m | grep helperApp
And since i now have the PID identified and acquired i was able to kill the process.
Thanks to everyone who helped.
Related
Find running apps in windows which can be seen in task manager under Apps Section. I want java code for getting that particular Apps list. i am able to get whole running background processes but i want running APPLICATION which shows under Apps tab in Task manager.
You can try with "qprocess" on cmd --> easy find command on gg ru run on java
I have a .jar runnable running on my server. When I run the file locally I am able to see its output via my IDE. Similarly I can connect via SSH and run the file and see the output, but when I close the session the JAR exits.
Is there any way to have my application continuously running and then tapping into the java applications output using a terminal service like SSH without having to stop/start the application.
Any help would be appreciated.
You can use either screen or nohup
What is happening is that when you close your SSH session, there is a hangup call made to the program.
You could use nohup. Nohup stands for "no hangup"
nohup java -jar myJar.jar > outputFile.txt
That would run the program in the backround and send all output to outputFile.txt. When you end your session it will continue running. You must use a kill command to kill it.
The second option is you could use screen which essentually creates a detachable "ghost session". When you run screen it looks like you are in a different ssh session. Then when you detach from screen, the processes continues in the backround. When you exit your ssh session, screen continues to run.
You simply ssh back into the server, and re-attach to your screen session, and like magic, your program is still running with all relevent output. A simple read on the man page should catch you up on how to do this.
man screen
Lastly, I decided to add this third option, not because its viable, but simply so you can understand that it is an option. (Some people would claim this is the only REAL option as it is what your SUPPOSED to do. Frankly I couldn't care less and think you should do whatever is the easiest to get to your goal.)
You can also edit your program to swallow the hangup signal. The program would then always run in the backround. You can then use
java -jar myJar.jar & > outputFile.com
The & tells the command to start a new process (aka run in the backround) and the > sends the output to a file. This is how normal server applications like tomcat or spring boot work. They simply swallow the hangup call.
i'm a bit lost with my application.
I run only one swing application (one window) but in the task manager i have two processes : java.exe and javaw.exe bopth consuming resources.
I found that javaw.exe is used when there's no java console, that's the case of my app.
So i'm wondering why is a "java.exe" process running and used by my application?
My app is launched by an exe (by launch4j) maybe it comes from that?
thanks
Most likely launch4j runs a java.exe console, which in turns starts a javaw.exe for running your GUI application. java.exe is for console applications only.
I want to install a monitoring system on a computer (the program is a jar file) and run it on start up every time any user logs on. However, I don't want the user to be able to terminate it since then it won't be able to be monitored any longer.
We have tried several ways:
Installing it as a service - the problem here is that our program doesn't work any longer; it can't connect to the computer that's monitoring it. We used "Yet Another Java Service Wrapper" for this, and looked into some other wrappers as well that could help us install it as a service.
Running the program on start up (using the folder startup), but not giving the basic user the privileges to edit/delete/mess around with the files. However, this seems to slow the whole computer down? This doesn't happen when we run the bat file executing the jar directly. Another issue with this is that the user can just go to the task manager and kill the java process.
We tried a variation of the previous one to solve the issue of the process being killed, by having another process. One will spawn the other and these 2 processes will keep tabs on each other. If one terminates, the other detects it and runs it to start it up again. Although it can have issues if the user is fast enough in killing both processes before either is respawned again, the bigger issue is that it sometimes has problems with connecting to other computers. We didn't have this problem when it was just 1 jar.
Does anyone have any idea on how these problems can be solved?
The context here is windows, but if you have suggestions for linux and mac that would be nice too!
Way to go is to run the program as a service. You should investigate any trouble between your application and your system's firewall. If you have windows firewall activated, you should add an exception for java.exe or javaw.exe.
In order to give elevated privileges to your program, you can set the service to run as another user. You can do this from the "Log on" tab in the service properties.
You'll want to have the program started under a user with elevated permissions. On WIndows this would the the Administrator, linux would use root. On Windows, its likely that you will need to start it as a service. But I really don't know why that would hinder the network communications.
I open up an external application from my java application. How can I close this application from the same Java application?
thanks
The best you can do (without venturing into messy/complicated/platform specific stuff) is to call Process.kill() on the Process object you got when you started the external application.
I don't think this is guaranteed to close the application*, and there is a chance that it may cause it to close uncleanly; i.e. without giving the application a chance to save open files, etc.
* Indeed, on *NIX if you started a "setuid root" process from a non-root Java application, and the OS won't let it send any signals to to.
Why don't you have a batch (Windows) or script (*nix) file that start and stops that application, and then run your runtime.exec with the parameter start/stop?
UPDATE:
This may help: http://it.toolbox.com/blogs/database-solutions/kill-a-process-in-windows-bat-19875
Second Update
You can also search by exe name using: 'tasklist ^| findstr /i excel.exe'
On Windows this will fail. It's a top 25 bug (or maybe top 25 rfe), though it really isn't so much Java's fault ... On Windows any children of the parent process will not be killed when parent is killed..... and there are many ways to run afoul of this (cmd /c anything and you will be in game-over-land)