multithreading - Java : Running shell script in background -
im trying run shell script in background not terminate when function/process end. seems despite nohup, when java thread ends. script.
below sample code.
/// /// tries run script in background /// try { runtime rt = runtime.getruntime(); process pr = rt.exec("nohup sh ./runner.sh > output.txt &", new string[] {}, wrkdir); // pr.waitfor(); // intentionally not using } catch(exception e) { throw new runtimeexception(e); }
nohup
applies invocations shell, not calling program.
there lot of ways solve this, 1 springs mind try modifying java code invoke launcher shell script invokes nohup runner.sh...
code (without needing launch sh
).
something (untested) code:
revised java:
/// /// tries run script in background /// try { runtime rt = runtime.getruntime(); process pr = rt.exec("sh ./launcher.sh", new string[] {}, wrkdir); // pr.waitfor(); // intentionally not using } catch(exception e) { throw new runtimeexception(e); }
launcher.sh
#!/bin/sh nohup ./runner.sh > output.txt &
i'm not sure redirection here, if works (as mentioned, solution untested), downside lose feedback attempting invoke runner
--any errors in script invocation unavailable java process.
Comments
Post a Comment