10

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.

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.

But the file's permission is -r-xr-xr-x. It does not provide permissions to write.

Can "Text file busy" happen when two processes trying to execute a perl file in the same time?

Community
  • 1
  • 1
joewhitedelux
  • 169
  • 1
  • 2
  • 9
  • (this is arguably more of a system administration question -- it depends on OS-specific semantics and isn't in any way specific to Perl, or even to interpreted languages -- so in the future, Server Fault may be the more appropriate venue) – Charles Duffy May 17 '12 at 15:29

2 Answers2

12

No, this won't happen simply because two Perl scripts are executing at the same time.

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.

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.

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

You can use the fuser command to see who has a file open, either for your script or for its interpreter:

$ sudo fuser /usr/bin/perl -uv
                     USER        PID ACCESS COMMAND
/usr/bin/perl:       root      16579 f.... (root)python
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thanks for your expansion, Charles. This perl file is executed by root under a clearcase view. After reading your post, I guess the clearcase view causes this problem, the actual perl file dose not provide writing permissions, but the file in clearcase view might provides. – joewhitedelux May 17 '12 at 15:30
2

But the file's permission is -r-xr-xr-x. It does not provide permissions to write.

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:

#!/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)
jfs
  • 399,953
  • 195
  • 994
  • 1,670