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).
Jared source
share