Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java executing bash script, error=26 Text file busy

Tags:

java

linux

bash

I've got a java code that is writing a Linux bash script out, then doing a chmod to add execute permission, then trying to execute it. I'm getting an IOException during the start of the process saying error=26, Text file busy. I've verified that the file is finished being written and the stream was closed. The chmod works fine, but I keep getting this error.

I've noticed that if I run a debugger and step through the code, it doesn't get the error, so clearly there is a timing issue involved. How can I make sure the chmod is done before I try to execute the bash script? I'd like to avoid non-reliable solutions like adding Thread.sleep(10000), and "hacky" things like putting the execution in a try/catch block inside a loop that tries until it succeeds.

I have a fair amount of code wrapping the startup of the process with listening threads, etc., but here is a simplified version of what it is doing (tried this code also and it has same result):

String[] cmd1 = {"/bin/chmod", "750", postFile };
new ProcessBuilder(cmd1).redirectErrorStream(true).start().waitFor();
String[] cmd2 = { postFile };
new ProcessBuilder(cmd2).redirectErrorStream(true).start().waitFor();

Every time after execution, the "postFile" has the correct 750 permissions, but it has not executed (due to the IOException).

like image 639
Jared Avatar asked Jun 14 '11 15:06

Jared


2 Answers

Are you sure it is the chmod that is responsible for the subsequent error? Could you check that you definitely close the output file before you try to run it?

If you do close it then I'm at a loss why chmod should cause that error, but you could avoid the need to run chmod by using your shell to run the script:

String[] cmd = {"bash", postfile };

like image 199
Paul Cager Avatar answered Oct 25 '22 15:10

Paul Cager


For future reference, it may have been caused by an unclosed stream in this particular case, but setting permissions on a file immediately followed by running the file can cause this error too:

java.io.IOException: Cannot run program "...": error=26, Text file busy

It is a probable bug in JDK. In my case, it was caused by this snippet of code

Files.setPosixFilePermissions(Paths.get(scriptPath), set(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.OWNER_READ));
ProcessBuilder processBuilder = new ProcessBuilder(scriptPath).directory(workingDir);
processBuilder.start();

even if nothing was editing the script file.

like image 23
Mifeet Avatar answered Oct 25 '22 17:10

Mifeet