java executing bash script, error=26 Text file busy

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

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).

回答1:

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 };



回答2:

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.



回答3:

I don't know if it's related but usually you need to get or redirect the ErrorStream and the InputStream (I usually get them in a ResponseStreamReader that I create, don't know about the redirecting choice).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!