-2

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

4
  • So what have you tried? Where are you stuck? Commented Oct 29, 2013 at 14:18
  • 1
    You need a process. See How to execute system commands (linux/bsd) using Java Commented Oct 29, 2013 at 14:19
  • 1
    see here stackoverflow.com/questions/18545921/run-linux-script-from-java/… Commented 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 directory Commented Oct 29, 2013 at 15:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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
Check out the processbuilder for ProcessBuilder.directory() docs.oracle.com/javase/7/docs/api/java/lang/…
@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!
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.