bash java执行bash脚本,错误= 26文本文件忙
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6345660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java executing bash script, error=26 Text file busy
提问by Jared
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.
我有一个 Java 代码,它正在编写 Linux bash 脚本,然后执行 chmod 以添加执行权限,然后尝试执行它。我在进程开始时收到一个 IOException,说 error=26, Text file busy。 我已经验证文件已完成写入并且流已关闭。chmod 工作正常,但我不断收到此错误。
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.
我注意到,如果我运行调试器并逐步执行代码,则不会出现错误,因此很明显涉及到时间问题。在尝试执行 bash 脚本之前,如何确保 chmod 已完成?我想避免不可靠的解决方案,例如添加 Thread.sleep(10000),以及“hacky”的事情,例如将执行放在循环中的 try/catch 块中,该循环会一直尝试直到成功。
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).
每次执行后,“postFile”都有正确的 750 权限,但它没有执行(由于 IOException)。
回答by Mifeet
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
这是JDK 中的一个可能的错误。就我而言,这是由这段代码引起的
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.
即使没有编辑脚本文件。
回答by Paul Cager
Are you sure it is the chmod that is responsible for the subsequent error? Could you check that you definitely closethe output file before you try to run it?
您确定是 chmod 对随后的错误负责吗?在尝试运行它之前,您能否检查一下您是否确实关闭了输出文件?
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:
如果您确实关闭了它,那么我不知道为什么 chmod 会导致该错误,但是您可以通过使用 shell 运行脚本来避免运行 chmod:
String[] cmd = {"bash", postfile };
String[] cmd = {"bash", postfile };
回答by talnicolas
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).
我不知道它是否相关,但通常您需要获取或重定向 ErrorStream 和 InputStream (我通常在我创建的 ResponseStreamReader 中获取它们,不知道重定向选择)。

