Kill process in Java - java

I run process from my Java code like this p = run.exec("cmd /c start \"\" C:\\<nameof .cmd file>");. At some point, I want to kill this process. Calling destroy() method on process kill the process, but I want to turn off command line, where procces is still running. When I looked to Task Manager, this process has no name, it has only postfix .exe.
In Task Manager, it look like this:
So I cannot do this p = run.exec("taskkill /F /IM <nameofexe>.exe");, because this running process dont have name.
Is there a way, how to completely turn off cmd and kill this running process?

When you launched your process, the CMD call may have launched additional child processes. Odds are good your second command line is killing one of the children, but not the CMD itself. The ideal situation would be to kill the launched process, not to run a second command line executable to kill (possibly) one of the children.
Process child = run.exec("cmd /c start \"\" C:\\<nameof .cmd file>");
if (timeToKillTheProcess) {
child.destroy();
child.waitFor();
}

Related

Open a new terminal window and execute command in it by java without using bash script

For examples, this is my bash script
osascript -e 'tell app "Terminal"
do script "./process1"
end tell'
osascript -e 'tell app "Terminal"
do script "./process2"
end tell'
Basically, it will open two different terminal windows (on mac) and execute defined commands. I try to do this in java by
process1 = Runtime.getRuntime().exec(new String[]{"process1"});
process2 = Runtime.getRuntime().exec(new String[]{"process2"});
The problem is it seems that there is only one terminal is opened (and not visible - it runs in background) and then two command process1 and process2 are executed. But because the process 1 will keep that terminal busy thus process2 cannot run. That's why I want to open different terminal to execute those commands.
Create a thread for each one of them. and give a time space "sleep(for some time thread 1 or 2)" and this will run both depending on you operating system.

Waiting for batch-end

I have 4 lines in my bat file, but my thread goes on before the cmd closes.
Here's my code:
rt = Runtime.getRuntime();
proc = rt.exec("cmd /c start C:\\temp\\test.bat");
if(proc.waitFor() == 0) {
return "did it";
} else {
return "nooope";
}
I always get the did it before cmd closes.
Here is my batch-file:
#ECHO off
taskkill /IM "Process.exe" /F
cd "C:\Program Files\ProcessFolder"
START /WAIT Process.exe
START otherProcess.exe
EXIT
any help?
The problem is that you're using start - which will start a new shell in which to run the batch file. The original shell then closes, so the process terminates, and your Java program continues.
Remove the start and it should work, in terms of waiting for the batch file to end. However, you've then got the same problem again within the batch file when you start otherProcess.
Either don't use start, or always use start /wait within the batch file.
If you use start /wait within the Java code, however, you'll end up with a command prompt sitting there at the end of the batch file execution, as far as I can tell by experimentation.

Starting and killing java app with shell script (Debian)

I'm new to UNIX. I want to start my java app with a script like so:
#!/bin/sh
java -jar /usr/ScriptCheck.jar &
echo $! > /var/run/ScriptCheck.pid
This is supposedly working. It does run the app and it does write the pid file. But when I try to stop the process with a different script which contains this:
#!/bin/sh
kill -9 /var/run/ScriptCheck.pid
the console gives me this error:
bash: kill: /var/run/ScriptCheck.pid: arguments must be process or job IDs
My best guess is that I'm not writing the right code in the stop script, maybe not giving the right command to open the .pid file.
Any help will be very appreciated.
You're passing a file name as an argument to kill when it expects a (proces id) number, so just read the process id from that file and pass it to kill:
#!/bin/sh
PID=$(cat /var/run/ScriptCheck.pid)
kill -9 $PID
A quick and dirty method would be :
kill -9 $(cat /var/run/ScriptCheck.pid)
Your syntax is wrong, kill takes a process id, not a file. You also should not be using kill -9 unless you absolutely know what you are doing.
kill $(cat /var/run/ScriptCheck.pid)
or
xargs kill </var/run/ScriptCheck.pid
I think you need to read in the contents of the ScriptCheck.pid file (which I'm assuming has only one entry with the PID of the process in the first row).
#!/bin/sh
procID=0;
while read line
do
procID="$line";
done </var/run/ScriptCheck.pid
kill -9 procID
I've never had to create my own pid; your question was interesting.
Here is a bash code snippet I found:
#!/bin/bash
PROGRAM=/path/to/myprog
$PROGRAM &
PID=$!
echo $PID > /path/to/pid/file.pid
You would have to have root privileges to put your file.pid into /var/run --referenced by a lot of articles -- which is why daemons have root privileges.
In this case, you need to put your pid some agreed upon place, known to your start and stop scripts. You can use the fact a pid file exists, for example, not to allow a second identical process to run.
The $PROGRAM & puts the script into background "batch" mode.
If you want the program to hang around after your script exits, I suggest launching it with nohup, which means the program won't die, when your script logs out.
I just checked. The PID is returned with a nohup.

Change process groups on Runtime.getRuntime().exec processes

I need to be able to start and stop an external program from inside java. I have the starting working just fine but when I stop it, it kills its parent. It turns out that the process I'm starting is killing its entire process group with a kill 0. Does anyone know how to make it so that my java process is not in the process group of the child program?
So I guess there are two answers:
1) Create your child process as the group leader of a new process group. In Linux I can do this on the command line by using
bash -c "command <args>"
Then you can check that the process group of the new process is different from the terminal you ran the command in with the command:
ps -efj
The 'j' option shows the process group id (PGID).
I will warn you that you may have to escape things weirdly to have it run correctly from Java because the quotes are required for commands with arguments that are passed to the 'bash' command with the '-c' option. So in Java I would guess it would look something like this:
Process processWithNewProcessGroup = Runtime.getRuntime().
exec("bash -c \"sleep 60\"");
2) Change your child process so it doesn't kill everything in its process group.

restart java app in ubuntu

What's the best way to restart a java app in ubuntu? I know u can run commands like this one from the terminal, but it doesnt seem to be working...
String restartArgs = "java -jar \'/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar\' --Terminal=true";
Process restart = Runtime.getRuntime().exec(restartArgs);
System.exit(1);
You are killing the parent process with System.exit(1), so its child process is destroyed as well.
To restart you would typically provide a shell script wrapper to launch the actual Java app.
#!/bin/sh
restartCode="1"; # predefined restart signal code
java -jar '/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar' --Terminal=true; # run java program
if [ $? -eq restartCode ] # if exit code is equal to predefined restart signal code
then
$0; # restart script
fi
exit $?;
Note the above code is a rough, crude outline. Typical wrappers are far more complex to deal with commandline arguments passed to the startup script itself etc. etc. Plus, my sh-skills are not infallible.
try providing full path for JAVA_HOME (e.g /usr/lib/jvm/java-6-sun/bin/java instead of java). The exec does not have Shell enironment variables.
also use
restart.waitFor(); //wait until process finishes
to make sure Java does not exit before the process finishes.
If you do want to run in shell (and use shell specific stuffs like pipe and ls) do this:
List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add("java -jar /home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar");
SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
int result = commandExecutor.executeCommand();
commandExecutor.waitFor(); //wait until process finishes

Categories