I want to run linux script from Java program and continue to execute program only when script stop. I am not interested to read script output ... Can anybody help me? Thanks a lot, and excuse me for my bad English
-
So what have you tried? Where are you stuck?Joachim Sauer– Joachim Sauer2013-10-29 14:18:42 +00:00Commented Oct 29, 2013 at 14:18
-
1You need a process. See How to execute system commands (linux/bsd) using Javasummerbulb– summerbulb2013-10-29 14:19:41 +00:00Commented Oct 29, 2013 at 14:19
-
1see here stackoverflow.com/questions/18545921/run-linux-script-from-java/…A4L– A4L2013-10-29 14:21:07 +00:00Commented Oct 29, 2013 at 14:21
-
Excuse me for bad formed question, my real problem that before I can execute my script i have to go to specific directory. If my script path is /quota_users/home/Hier_CarryReverse/syn/runme first i have to execute cd /quota_users/home/Hier_CarryReverse/syn/ and then ./runme. I am not succeed with rolfl example i have an error that says Cannot run program "cd /quota_users/home/Hier_CarryReverse/syn": java.io.IOException: error=2, No such file or directorys0ld13r– s0ld13r2013-10-29 15:10:33 +00:00Commented Oct 29, 2013 at 15:10
Add a comment
|
2 Answers
Assuming all other threads are idle:
// run the script.
Process proc = Runtime.getRuntime().exec("/path/to/myscript");
// wait for the return code.
int ecode = proc.waitFor();
If you have more complex arguments to your script, or it needs to monitor STDOUT, STDERR, or needs other modifications (like feeding data to STDIN, or changing execution directory, environment variables, etc.) then you should do the same effective procedure, but instead of using Runtime.exec(...) you should build and start the Process manually. Read the Process javadoc and ProcessBuilder javadoc on how to set it up, and start it.
5 Comments
Joachim Sauer
This will only work if the script has no (or very little) output. Otherwise it will block and wait for something to read that output.
rolfl
The OP's q appears to be a really simple one.... an assumption, I admit, so I have edited to add references to where to look for more information for more complicated cases.
s0ld13r
Excuse me for bad formed question, my real problem that before I can execute my script i have to go to specific directory. If my script path is /quota_users/home/Hier_CarryReverse/syn/runme first i have to execute cd /quota_users/home/Hier_CarryReverse/syn/ and then ./runme. I am not succeed with rolfl example i have an error that says Cannot run program "cd /quota_users/home/Hier_CarryReverse/syn": java.io.IOException: error=2, No such file or directory
rolfl
Check out the processbuilder for ProcessBuilder.directory() docs.oracle.com/javase/7/docs/api/java/lang/…
Joachim Sauer
@s0ld13r: then please update your question instead of writing this in a comment on a random answer. Others have pretty much no chance of finding this updated requirement!