Java bash script execution, error = 26 Text file busy

I have Java code that writes a Linux bash script, and then does chmod to add execute permission, and then tries to execute it. I get an IOException during the start of the process saying error = 26, the text file is busy. I checked that the file was finished and it was closed. chmod works fine, but I keep getting this error.

I noticed that if I run the debugger and go through the code, it will not receive an error, so it is clear that there is a problem with synchronization. How can I make sure chmod is done before I try to run a bash script? I would like to avoid unreliable solutions like adding Thread.sleep (10000) and “hacking” things like executing execution in a try / catch block inside a loop that tries until it succeeds.

I have a sufficient amount of code that completes the start of the process using listener streams, etc., but here is a simplified version of what it does (I tried this code also had the 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();

Each time after executing, "postFile" has the correct 750 permissions, but it is not executed (due to an IOException).

+3
source share
3

, chmod ? , , ?

, , chmod , chmod, script:

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

+4

, , , , :

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

JDK.

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

script.

+2

I don’t know if this is related, but usually you need to get or redirect ErrorStream and InputStream (usually I get them in the ResponseStreamReader that I create, I don’t know about the choice of redirection).

0
source

All Articles