Linux 当两个进程试图同时执行一个 perl 文件时,会发生“文本文件繁忙”吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10638283/
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
Can "Text file busy" happen when two processes trying to execute a perl file in the same time?
提问by joewhitedelux
I got this message - "Text file busy" when I try to execute a perl file while this file is currently being used by another processes.
当我尝试执行 perl 文件而该文件当前正被另一个进程使用时,我收到此消息 - “文本文件繁忙”。
According to this /usr/bin/perl: bad interpreter: Text file busy, this problem happens when the perl file is open for writing when I try to execute it.
根据此/usr/bin/perl: bad interpreter: Text file busy,当我尝试执行 perl 文件以进行写入时,会发生此问题。
But the file's permission is -r-xr-xr-x. It does not provide permissions to write.
但是文件的权限是-r-xr-xr-x。它不提供写入权限。
Can "Text file busy" happen when two processes trying to execute a perl file in the same time?
当两个进程试图同时执行一个 perl 文件时,会发生“文本文件繁忙”吗?
回答by Charles Duffy
No, this won't happen simply because two Perl scripts are executing at the same time.
不,这不会仅仅因为两个 Perl 脚本同时执行而发生。
The more likely explanation is that the script itself is open for write while the operating system is trying to read its shebang line to determine the interpreter to use.
更可能的解释是,当操作系统试图读取它的 shebang 行以确定要使用的解释器时,脚本本身是开放的。
This can also happen if an external process is trying to upgrade or modify the Perl interpreter itself, or one of the shared libraries it depends on. Note that file permissions don't generally apply to superuser accounts, such as root, so any process running as superuser can still attempt to modify the Perl interpreter despite there being no +w
bits set.
如果外部进程试图升级或修改 Perl 解释器本身或其所依赖的共享库之一,也会发生这种情况。请注意,文件权限通常不适用于超级用户帐户,例如 root,因此任何以超级用户身份运行的进程仍然可以尝试修改 Perl 解释器,尽管没有+w
设置位。
(That said, most well-behaved operating system upgrade tools on POSIX-style operating systems will write the upgraded version of a binary to a new file on the same filesystem, close that file when done, and rename it over the original (an atomic operation) -- such that the inode attached at /usr/bin/perl
is itself never open for write. As such, on a well-behaved system, the error you're seeing isn't something that should ever come up in practice).
(也就是说,POSIX 风格的操作系统上大多数表现良好的操作系统升级工具会将二进制文件的升级版本写入同一文件系统上的新文件,完成后关闭该文件,并在原始文件上重命名(原子操作)——这样附加的 inode/usr/bin/perl
本身永远不会打开写入。因此,在行为良好的系统上,您看到的错误在实践中不应该出现)。
You can use the fuser
command to see who has a file open, either for your script or for its interpreter:
您可以使用该fuser
命令查看谁打开了您的脚本或其解释器的文件:
$ sudo fuser /usr/bin/perl -uv
USER PID ACCESS COMMAND
/usr/bin/perl: root 16579 f.... (root)python
回答by jfs
But the file's permission is -r-xr-xr-x. It does not provide permissions to write.
但是文件的权限是-r-xr-xr-x。它不提供写入权限。
The permission can be set after you open the script for writing but before the script is run.
可以在打开脚本写入后,运行脚本前设置权限。
Here's code example that writes a new perl script your-script
in the current directory, makes it executable while removing the write permissions, and tries to run the perl script. The final permissions are -r-xr-xr-x
but the file is still opened for writing that is why the script generates "Text file busy"error:
下面的代码示例your-script
在当前目录中编写一个新的 perl 脚本,在删除写入权限的同时使其可执行,并尝试运行 perl 脚本。最终权限是-r-xr-xr-x
但文件仍然打开进行写入,这就是脚本生成“文本文件繁忙”错误的原因:
#!/usr/bin/env python3
import os
import stat
import subprocess
file = open('./your-script', 'w') # specify full path
try:
file.write("#!/usr/bin/perl\nprint 'unreachable';")
file.flush() # make sure the content is sent to OS
os.chmod(file.name, 0o555) # make executable
print(stat.filemode(os.stat(file.name).st_mode)) # -r-xr-xr-x
subprocess.call(file.name) # run it
except Exception as e:
print(e)
finally:
os.remove(file.name)