[NB. This is related to How do I launch a completely independent process from a Java program? but different]
I want to be able to spawn external processes (shell scripts) from a "manager" Java process that should keep running when the JVM is killed - but it seems that when I kill the parent Java program the child is killed too (note, the behaviour is different if the JVM exits naturally). The simplest test program I have is:
public class Runit {
public static void main(String args[]) throws IOException, InterruptedException {
Runtime.getRuntime().exec(args[0]);
// doesn't work this way either
// ProcessBuilder pb = new ProcessBuilder(args[0]);
// pb.start();
while (true) {
System.out.println("Kill me");
Thread.sleep(2000);
}
}
}
and external script:
#!/bin/sh
while [ 1 ] ; do
ls
sleep 1
done
run as
java -classpath jar-with-dependencies.jar temp.exec.Runit runit.sh
If the manager simply exits (i.e. take out the "while" loop in the Java program) then the spawned process keeps running, but when I Ctrl+c the Java program the external program is killed too which is not what I want.
I'm using OpenJDK 1.6 on Ubuntu.
Edit1: Changing the exec to
Runtime.getRuntime().exec("/usr/bin/nohup " + args[0]);
doesn't help.
Edit2: Adding a shutdown hook as described in How to gracefully handle the SIGKILL signal in Java doesn't stop the Ctrl+c being propagated to the child.
Vladimir gave the hint we needed! (Sorry, beat Lukasz to it)
Add another script spawn_protect.sh
#!/bin/sh
LOG=$1
shift
nohup $* > $LOG 2>&1 &
And change the manager to:
public class Runit {
public static void main(String args[]) throws IOException, InterruptedException {
Runtime.getRuntime().exec(args);
while (true) {
System.out.println("Kill me");
Thread.sleep(5000);
}
}
}
Then run as:
java -classpath jar-with-dependencies.jar temp.exec.Runit spawn_protect.sh /tmp/runit.log runit.sh
Now runit.sh is really detached from the JVM process!
In Linux, if you start another process, it is your child and you are his parent. If parent gets killed, all children get killed, and their children too (what a terrible atrocity).
What you need, is to start a process that won't be killed when you exit your program. So, you need to give birth to not your own child. The methods to do that are described for example here: Linux: Prevent a background process from being stopped after closing SSH client for example use screen utility.
You've got to make it a daemon. Don't be afraid it's not a horror movie. Simply you'll need to detach your processes from controlling terminal session. I've always do it in a oposite way: shell script that launches Java.
Here is an explanation:
http://en.wikipedia.org/wiki/Daemon_(computing)
You can also you "jvm shutdown hooks", but they will not work in some situations.
Related
In my code, I want to restart the program. For this i have used the following code in Windows:
if(System.getProperty("os.name").contains("Windows"))
{
//For Windows Builds use this
new ProcessBuilder("java", "Launcher").inheritIO().start();
System.exit(0);
}
For Linux Builds I used
else
{
//For Linux/Unix or Mac Builds use this
new ProcessBuilder("/bin/bash", "-c" ,"java Launcher").inheritIO().start();
}
So now, the implementation for Windows works just fine. It begins a new instance and exits the old.
But the Linux implementation is kinda a bit odd. I added System.exit(0); thinking that it will kill the current process right after creating the new one, but it seemed to exit the process itself. I cannot restart the program in anyway in Linux, although it was doable in Windows.
Would appreciate help and feedback!
EDIT: [28-July-2020]
So I did find that the new process is created, but the IO is not inherited to the new session. I tweaked a bit of code and now the program creates the new process, gets IO control and after entering a command, it exits.
if(System.getProperty("os.name").contains("Windows"))
{
//For Windows Builds use this
new ProcessBuilder("cmd", "/c", "java Launcher").inheritIO().start();
System.exit(0);
}
else
{
//For Linux/Unix or Mac Builds use this
long pid = ProcessHandle.current().pid();
System.out.println(pid);
String a=String.valueOf(pid);
Thread.sleep(10000);
System.out.println(new ProcessBuilder("/bin/bash", "-c", "java Launcher").inheritIO().start());
System.exit(1);
}
Without System.exit(1); the program continues with the newly created process, but with the old process still running in the background. When I try to kill the old process, both the processes are killed.
Here are the new screenshots, with the code specified above.
https://gofile.io/d/MAYLeJ
EDIT: [29-July-2020]
Been working more on why the code is not working. I did get an exception for the same code, which WSL didnt detect!
The Error Log
Update: I did find the right answer and it might be a bit complex, but I shall try to make it as simple as possible.
In this, we will require 2 separate classes: 1 class to monitor the child process, 1 class which is the main process.
For the sake of simplicity, I shall name the monitoring class as SessionManager and the main class as mainClass
In the SessionManager class, I've implemented the following code.
try
{
//A loop which will run infinitely to montior and start new processes
while(true)
{
//Create the process and listen to the exit code generated by the child process spawned.
ProcessBuilder session_monitor=new ProcessBuilder("java", "<classname>");
Process process_monitor= session_monitor.inheritIO().start();
//wait for the child process to end before proceeding
process_monitor.waitFor();
switch(process_monitor.exitValue())
{
//Implement the logic here
case 0: //do something
break;
case 1: //do something
break;
}
}
}
catch(Exception E)
{
E.printStackTrace();
}
And in the mainClass program, we shall have a main method to start the process running.
class mainClass
{
public static void main(String[] args)
{
System.out.println("Hello World");
//exit the process with a code for the SessionManager's logic to work
System.exit(0); //Using a normal exit code here
}
}
This has worked for me, and if you think this implementation can be improved, I'd love to hear it. Thank you for all your support :)
I am launching a process with Runtime.getRuntime().exec()
However, once Java reaches the end of the main() loop, java will not close, unless I destroy() the process first.
Problem with that is, I need the process to keep running after Java is closed.
I pretty much want to do this
public static void main(String args[]) {
Runtime.getRuntime().exec("file.bat");
// now I want java to close, and I want file.bat to keep running
}
I tried System.exit(), it will stop my main() loop, however I think a thread or something that was started by exec() keeps running, preventing java from closing. I can't even end it in Eclipse without first exiting file.bat
Why isn't it closing? And how would I fix it?
I couldn't find anything online, and I've been experimenting for a while, so I decided to ask you guys.
Thank you,
-Alex Benoit
Figured it out. I'll share my code in case anyone else has the same question. I brought it down to 1 line.
Desktop.getDesktop().open(new File("C:\\Folder\\File"));
This is a system dependent question. I am not sure if in Windows the parent process can terminate before the child. I believe this is true in linux.
As a guideline, you should call waitFor() on the process. However, on some systems, just doing so might not be enough. As pointed out in the javadoc, the out/err stream need to be properly purged (using the streams returned by getOutputStream() and getErrorStream()) because they could keep your process from completing. To do so, I found it most appropriate to use two separate threads, one purging the err stream, the other the out stream. The calling thread (main in your case), has to do the following:
start the external process;
start a thread to purge the out stream;
start a thread to purge the err stream;
call waitFor() on the calling thread (main).
I found the above approach very robust and easy to implement (if you are familiar with threads). Please use an ExecutorService for the threads.
Launch the file with cmd for Windows Only
Older windows will probably have to use command.com instead
Runtime.getRuntime().exec("cmd /c start file.bat");
Using the start with /wait parameter waits until bat is finished without /wait it should work.
public class Command {
public static void main(String[] args) throws java.io.IOException, InterruptedException {
String path = "C:\\DOCUME~1\\\USER\\DESKTOP";
Process p = Runtime.getRuntime().exec("cmd /c start /wait " + path + "\\test.bat");
System.out.println("Waiting for batch file ...");
p.waitFor();
System.out.println("Batch file done.");
}
}
test.bat
#echo off
cls
:start
echo This is a loop
goto start
Use the command.com file to get short path name, since long path names don't get processed too well in start program.
Here is a sample I made download link below:
https://www.mediafire.com/?mu7vht3e6tto698
Your problem may be that you are not on a Windows Administrator account you could try, But this requires you to type in your Administrator password which is very stupid.
Process p = Runtime.getRuntime().exec("runas /profile /user:Administrator \"cmd.exe /c start test.bat\"");
I start another application from within my Java app using runtime.exec. That child program starts no problems but when I call system.exit(0); from the parent app the parents JVM keeps running until the child program is terminated.
Here is a simple example:
public class Test {
public static void main(String[] args) {
try{
Runtime runtime = Runtime.getRuntime();
Process p=runtime.exec("cmd /c \"client.exe\"");
Thread.sleep(10000);
System.exit(0);
}
catch(Exception e){}
}
}
I want the child app to continue running after System.exit is called and for Test apps JVM to shutdown completely.
In eclipse I get this behavior:
After the Test app starts, client.exe starts up immediately. After the 10 second sleep the app exits but from the eclipse console the Test app is still running. More strange hitting the red terminate button at this point does nothing. Yet as soon as client.exe is closed the parent Test app finally terminates.
However even more strange if I hit the red terminate button in the eclipse console BEFORE the 10 second sleep elapses then the Test App closes and the client.exe keeps on running. This is the behavior I want but I can't figure out. Any ideas?
This is the default behaviour of most OS's, that a parent process can't terminate until all child processes have terminated.
Now, there is a "funky" way to create a "parentless" process, so that the parent process can terminate and the child process will continue running, but this becomes OS dependent, which you already have any way...
Basically, the command lines becomes something like...
cmd /C start /B /NORMAL cmd /c "client.exe"
Now. I would recommend that you use the Runtime#exec(String[]) variant, as this deals with spaces within commands better
String[] cmd = {"cmd", "/C", "start", "/B", "/NORMAL", "cmd", "/c", "client.exe"};
runtime.exec(cmd);
Normally, I would recommend ProcessBuilder over Runtime#exec, as it allows you to, amongst other things, change the working directory for the process...
In "C", I can run a long blocking process in the background (AND HAVE IT CONTINUE TO RUN) after the starting process has exited.
void main(void)
{
system("some_long_blocking_process &");
exit();
}
// "some_long_blocking_process" is still running here (DESIRED BEHAVIOR)
Java's getRuntime().exec() DOESN'T have this behavior. Instead, "some_long_blocking_process" ends immediately when the Java process ends.
Anyone know how I can recapture this behavior in Java?
I am using Java 1.4 (No process builder)
I specifically am looking to start the long blocking process and to exit immediately (no "waitFor(), etc.)
Things I have already tried (the process runs correctly, but I still get the same undesired behavior)
adding "nohup" and run in foreground ("nohup some_long_process")
adding "nohup" and running in background ("nohup some_long_process &")
run in foreground ("some_long_process")
run in background ("some_long_process &")
THANKS!
Thanks to all the suggestions... I've decided to use jtahlborn's answer (it worked for me)
try this:
String[] cmd = {"/bin/sh", "-c", "yourcommand args"};
Process p = Runtime.getRuntime().exec(cmd);
when redirect stream to /dev/null:
String[] cmd = {"/bin/sh", "-c", "yourcommand args > /dev/null 2>&1 &"};
Process p = Runtime.getRuntime().exec(cmd);
the only way we were able to achieve this with java was to add another layer of script. you need a simple wrapper script which invokes the app you actually want to run, e.g.:
runner.sh:
#!/bin/sh
nohup "$#" > /dev/null 2>&1 &
then invoke "/bin/sh runner.sh the real command" from your java program.
EDIT:
Have your tried this?
Runtime.getRuntime().exec("/bin/sh -c /usr/X11/bin/xterm &")
This worked for me on MacOS.
Previous answer (JDK 1.5, apologies for not reading the question correctly):
To execute a process without waiting you can use the ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("/usr/X11/bin/xterm");
pb.start();
Your problem is probably due to the trailing &. Try removing it.
getRuntime().exec() is more similar to fork() and exec() than system().
system() passes the command to the shell, and it's Bash that understands that the trailing ampersand means to run the process in the background.
getRuntime().exec() parses the command using a StringTokenizer to parse the command, and doesn't do anything with the trailing ampersand. That's simply passed as the first argument to your some_long_blocking_process, which may exit out immediately on the unknown error.
Have you tried spawning a new Thread to run the executable? Try:
new Thread(new Runnable() {
#Override
public void run() {
try {
Runtime.getRuntime().exec(<your exec>);
} catch (IOException e) {
e.printStackTrace();
}
}
}).run();
This way, the main process won't shutdown until the exec has finished running (and the thread has finished).
Also, don't manually call exit() in your application unless you have some overwhelming reason to--the JVM does a good job of detecting when the application has finished on its own. This way, you won't force close threads that are running in the background.
I am working on a program written in Java which, for some actions, launches external programs using user-configured command lines. Currently it uses Runtime.exec() and does not retain the Process reference (the launched programs are either a text editor or archive utility, so no need for the system in/out/err streams).
There is a minor problem with this though, in that when the Java program exits, it doesn't really quit until all the launched programs are exited.
I would greatly prefer it if the launched programs were completely independent of the JVM which launched them.
The target operating system is multiple, with Windows, Linux and Mac being the minimum, but any GUI system with a JVM is really what is desired (hence the user configurability of the actual command lines).
Does anyone know how to make the launched program execute completely independently of the JVM?
Edit in response to a comment
The launch code is as follows. The code may launch an editor positioned at a specific line and column, or it may launch an archive viewer. Quoted values in the configured command line are treated as ECMA-262 encoded, and are decoded and the quotes stripped to form the desired exec parameter.
The launch occurs on the EDT.
static Throwable launch(String cmd, File fil, int lin, int col) throws Throwable {
String frs[][]={
{ "$FILE$" ,fil.getAbsolutePath().replace('\\','/') },
{ "$LINE$" ,(lin>0 ? Integer.toString(lin) : "") },
{ "$COLUMN$",(col>0 ? Integer.toString(col) : "") },
};
String[] arr; // array of parsed tokens (exec(cmd) does not handle quoted values)
cmd=TextUtil.replace(cmd,frs,true,"$$","$");
arr=(String[])ArrayUtil.removeNulls(TextUtil.stringComponents(cmd,' ',-1,true,true,true));
for(int xa=0; xa<arr.length; xa++) {
if(TextUtil.isQuoted(arr[xa],true)) {
arr[xa]=TextDecode.ecma262(TextUtil.stripQuotes(arr[xa]));
}
}
log.println("Launching: "+cmd);
Runtime.getRuntime().exec(arr);
return null;
}
This appears to be happening only when the program is launched from my IDE. I am closing this question since the problem exists only in my development environment; it is not a problem in production. From the test program in one of the answers, and further testing I have conducted I am satisfied that it is not a problem that will be seen by any user of the program on any platform.
There is a parent child relation between your processes and you have to break that.
For Windows you can try:
Runtime.getRuntime().exec("cmd /c start editor.exe");
For Linux the process seem to run detached anyway, no nohup necessary.
I tried it with gvim, midori and acroread.
import java.io.IOException;
public class Exec {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("/usr/bin/acroread");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished");
}
}
I think it is not possible to to it with Runtime.exec in a platform independent way.
for POSIX-Compatible system:
Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "your command"}).waitFor();
I have some observations that may help other people facing similar issue.
When you use Runtime.getRuntime().exec() and then you ignore the java.lang.Process handle you get back (like in the code from original poster), there is a chance that the launched process may hang.
I have faced this issue in Windows environment and traced the problem to the stdout and stderr streams. If the launched application is writing to these streams, and the buffer for these stream fills up then the launched application may appear to hang when it tries to write to the streams. The solutions are:
Capture the Process handle and empty out the streams continually - but if you want to terminate the java application right after launching the process then this is not a feasible solution
Execute the process call as cmd /c <<process>> (this is only for Windows environment).
Suffix the process command and redirect the stdout and stderr streams to nul using 'command > nul 2>&1'
It may help if you post a test section of minimal code needed to reproduce the problem. I tested the following code on Windows and a Linux system.
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec(args[0]);
}
}
And tested with the following on Linux:
java -jar JustForTesting.jar /home/monceaux/Desktop/__TMP/test.sh
where test.sh looks like:
#!/bin/bash
ping -i 20 localhost
as well as this on Linux:
java -jar JustForTesting.jar gedit
And tested this on Windows:
java -jar JustForTesting.jar notepad.exe
All of these launched their intended programs, but the Java application had no problems exiting. I have the following versions of Sun's JVM as reported by java -version :
Windows: 1.6.0_13-b03
Linux: 1.6.0_10-b33
I have not had a chance to test on my Mac yet. Perhaps there is some interaction occuring with other code in your project that may not be clear. You may want to try this test app and see what the results are.
You want to launch the program in the background, and separate it from the parent. I'd consider nohup(1).
I suspect this would require a actual process fork. Basically, the C equivalent of what you want is:
pid_t id = fork();
if(id == 0)
system(command_line);
The problem is you can't do a fork() in pure Java. What I would do is:
Thread t = new Thread(new Runnable()
{
public void run()
{
try
{
Runtime.getRuntime().exec(command);
}
catch(IOException e)
{
// Handle error.
e.printStackTrace();
}
}
});
t.start();
That way the JVM still won't exit, but no GUI and only a limited memory footprint will remain.
I tried everything mentioned here but without success. Main parent Java process can't quit until the quit of subthread even with cmd /c start and redirecting streams tu nul.
Only one reliable solution for me is this:
try {
Runtime.getRuntime().exec("psexec -i cmd /c start cmd.cmd");
}
catch (Exception e) {
// handle it
}
I know that this is not clear, but this small utility from SysInternals is very helpful and proven. Here is the link.
One way I can think of is to use Runtime.addShutdownHook to register a thread that kills off all the processes (you'd need to retain the process objects somewhere of course).
The shutdown hook is only called when the JVM exits so it should work fine.
A little bit of a hack but effective.