Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

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?

like image 420
joewhitedelux Avatar asked May 17 '12 15:05

joewhitedelux


2 Answers

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
like image 123
Charles Duffy Avatar answered Sep 28 '22 07:09

Charles Duffy


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)
like image 31
jfs Avatar answered Sep 28 '22 07:09

jfs