Running Java application from C# properly - java

I am trying to execute Java application from my c# code. I use Process class from System.Diagnostics.
I am able to run it and kill, but it seems that java starts subprocess when I am executing application. And when I am trying to kill the Process, I kill the parent java process, the second one, which was started behind the scenes, is still running.
Multiple executions of this logic cause dozens of java.exe processes and memory overflow.
Is there a way to do such thing as proper running Java application from c# code and be able to kill started processes?
I wonder if it is possible at all to resolve this issue if I don't have administrative rights

I would be tempted to use taskkill. It runs from this cmd command:
string.Format("cmd /c \"taskkill /f /pid {0}\" /t", this.processId);
the /t does a tree kill, that ends the child processes.

Related

kill a process with name in windows machine

I am using "cygwin".
I know 'TASKKILL' allows to kill process. But it wont allow to kill by process name. I tried psKill , but it is giving error as not recognized. Some one please help me.If i want to kill 'test.py', how to do it. TASKKILL will kill all the python process running.
Assuming your question is related to your Cygwin environment only -- as opposed to the larger Windows process environment -- there are standard *nix type tools available in the Cygwin distribution to manage this. Processes are identified by unique ID numbers that you can kill with the kill command from your Cygwin shell prompt. Run the ps command to see a list of all processes currently running from Cygwin.
If your question is more involved than this, you might give this post a look:
How to kill a process in cygwin?
Good luck!

Find and kill a specific Java process from another Java App

I have several java processes running on a windows machine. I have a Java process which is supposed to monitor the other processes and periodically kill or restart new ones.
If I have a java process running com.foo.Main1 and one running com.foo.Main2 - how can my monitoring process find and kill just the Main2 process?
Update: I have some code that can execute a command line tasklist.exe and parse it, but no matter what I do, I only see the java.exe process, not which class is executing
Update 2: I do not have the ability to install non-java programs.
It's probably going to be a lot simpler using OS-specific tools and using Runtime.exec() to run them, but I'll try and give a platform independent answer:
It might be possible to do this platform independently using the Attach API. This comes with the JDK, so to use it just include tools.jar from your JDK on your program's classpath.
To get a list of virtual machines on the system, use VirtualMachine.list(). You can get/parse arguments from the virtual machine descriptor objects that are returned from this.
The attach API also allows you to load agents into already-running Java processes. Since you want to kill a Java process, you can write a Java agent that simply runs System.exit() (or if you really want it dead use Runtime.halt() instead) when the agent loads.
Once you identify the one you want to kill, attach to it and load the killer agent (the agent has to be built as a JAR file, accessible to the Java process it needs to be loaded into). Shortly after the agent is attached that process should die.
These links might help also:
An Oracle blog on the attach API
Package documentation for java.lang.instrument (has detailed instructions on how to build an agent JAR)
This is specific to Windows.
I was facing the same issue where I have to kill the specific java program using taskkill. When I run the java program, tasklist was showing the same program with Image name set as java.exe. But killing it using taskkill /F java.exe will stop all other java applications other than intended one which is not required.
So I run the same java program using:
start "MyProgramName" java java-program..
Here start command will open a new window and run the java program with window's title set to MyProgramName.
Now to kil this java-program use the following taskkill command:
taskkill /fi "MyProgramName"
Your Java program will be killed only. Rest will be unaffected.

How to terminate JVM run from shell script?

I have a shell script starting two java processes. How to ensure that when shell script process is killed, all It's children will be killed too? For example when I try to kill It, java processes remain alive:
kill -9 myscriptID
You can check for all java processes by running this:
ps -aux | grep *java*
This looks like it can help as well.
Your kill command only kills your script, not other processes that it has spawned. Best way to kill all child processes has some great answers for killing the whole tree.
That said, it seems that a better design would be to keep track of the PIDs of the child processes, and rather than sending the parent SIGKILL, send it SIGTERM (or have some other way) to trigger it to gracefully kill its children.

killing a bash process does not kill the process the bash is currently running

The scenario is as follows: I have a java daemon, which is supposed to not terminate. However, in case of an unexpected error, the crashed JVM should be restarted by a script. So I wrote a command which starts a background bash which has a loop starting the JVM (so when the JVM terminates, it will be restarted again).
/bin/bash -c "while true; do java ...; done" &
In order to be able to stop the daemon, I thought of killing this bash background process (by saving it's process id in a file). This works insofar as the background bash doesn't restart the JVM, but still doesn't kill the currently running process - so the bash seems to end it's current command before it checks for a kill command. I would like to have the currently running JVM to be killed, too.
Since I don't want to manage 2 PIDs (one for the background bash and one for the currently running JVM), is there a way of "force kill" which by design stops the current command? (I couldn't find such thing in man kill)?
There are a number of process-management tools built for exactly this purpose: runit, daemontools, upstart... even an entry in the SysV inittab table.
All of these will automate restarting immediately on shutdown, track desired status as opposed to current status (and attempt to signal startup or shutdown as-desired), manage signal delivery, etc.
You can trap signals in bash and trigger events on them, but that only handles the subset which can be trapped (you can't trap a KILL, for instance). The better thing is to use a tool built-to-purpose.
The ProcessManagement page of the wooledge.org wiki (used by irc.freenode.org's #bash channel) has some other concrete suggestions on doing this yourself in bash... though it too suggests runit, daemontools, and their kin as the best-practices approach.
Why not use cron to start your app, and manage only 1 pid, the one belonging to your app? That way you'll always be killing the correct process.
Emphasising a bit, you could create a bash script to manage your app: start|stop|status. On start it will save the java pid to a file. Then you can schedule a cron job to verify the status of the app, and if the pid does not exist, relaunch it.
Isn't this the default behaviour of bash? I thought for example zsh does the opposite and doesn't send a SIGHUP to all child process? Maybe you can try this answer and write a little script and start it with disown?
see this question: Tie the life of a process to the shell that started it
I didn't test it but I need zsh in my webserver because I start it manually and exit my shell with double CTRL-D.

java Process stop entire process tree

I am using Java Runtime to run commands, including certain CVS commands.
I use:
process = runtime.exec ("cmd /C cvs...");
format for running the Process in Java
I need to have the option of stopping it. For this I use the Java Process destroy method
process.destroy();
However only the cmd is stopped not the cvs process. It continues to run as a separate process without the cmd process as the parent. There are many references to this on the internet, but I haven't found any satisfactory solution. Thanks
This is a problem with the windows cmd shell. Why do you use it? Can't you do exec("cvs ...") instead?
It may be possible using Runtime.exec to get the PID of the process you have run. And with that you might be able to shut down the process tree.
You would however need 2 other programs to find the PID and to terminate the process tree.

Categories

Resources