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
Asked
Active
Viewed 1,362 times
-2
-
So what have you tried? Where are you stuck? – Joachim Sauer Oct 29 '13 at 14:18
-
1You need a process. See [How to execute system commands (linux/bsd) using Java](http://stackoverflow.com/a/792049/416300) – summerbulb Oct 29 '13 at 14:19
-
1see here http://stackoverflow.com/questions/18545921/run-linux-script-from-java/18546394#18546394 – A4L Oct 29 '13 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 directory – s0ld13r Oct 29 '13 at 15:10
2 Answers
2
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.

rolfl
- 17,539
- 7
- 42
- 76
-
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. – Joachim Sauer Oct 29 '13 at 14:24
-
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. – rolfl Oct 29 '13 at 14:29
-
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 – s0ld13r Oct 29 '13 at 15:24
-
1Check out the processbuilder for ProcessBuilder.directory() http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#directory(java.io.File) – rolfl Oct 29 '13 at 16:01
-
@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! – Joachim Sauer Oct 29 '13 at 18:17
1
You can also launch the bash interpreter instead
Process proc = Runtime.getRuntime().exec("/bin/bash /path/to/myscript");
int ecode = proc.waitFor();
This may work in some generally broken cases when @rolfl solution may not work (non executable script file, #!/ header missing, etc)

Audrius Meškauskas
- 20,936
- 12
- 75
- 93