The ETXTBSY error means a process is trying to modify a file that another process has open for execution, preventing the modification.

Common Causes and Fixes for ETXTBSY

  1. The ld.so.cache File is Being Updated:

    • Diagnosis: You’ll typically see this error when trying to update libraries or package lists. The ldconfig command is the usual suspect.
    • Command: sudo lsof /etc/ld.so.cache
    • Fix: Wait for the ldconfig process (or whatever is updating it) to finish. If it’s stuck, you can try to find the PID of the ldconfig process and kill it: sudo pkill ldconfig.
    • Why it works: ldconfig rebuilds the shared library cache. While it’s running, it locks ld.so.cache to prevent other processes from reading an inconsistent state. Modifying it during this time is blocked.
  2. A Running Program is Executing a Script or Binary that’s Being Modified:

    • Diagnosis: You’re trying to edit a script or binary file (e.g., a Python script, a compiled executable, a shell script) that is currently being executed by another process.
    • Command: sudo lsof | grep /path/to/your/file (Replace /path/to/your/file with the actual file path). Look for processes with txt in the FD column.
    • Fix: Identify the process ID (PID) from the lsof output and gracefully terminate it. For example, if the PID is 12345: sudo kill 12345. If it doesn’t stop, use sudo kill -9 12345.
    • Why it works: The kernel prevents a file marked as being executed (TXT flag in lsof) from being written to. This safeguards against the executable code changing mid-execution, which would lead to unpredictable behavior and crashes. Terminating the executing process releases the lock.
  3. NFS (Network File System) Mounts with Open Executables:

    • Diagnosis: If the file resides on an NFS mount, a process on another machine could be executing it.
    • Command: sudo lsof /mnt/nfs/path/to/file (where /mnt/nfs/path/to/file is the file on the NFS mount). The output might show a PID from a remote host.
    • Fix: You’ll need to identify the process on the NFS client (if you’re the server) or the NFS server (if you’re the client) that is holding the lock. Often, this means finding the user or service on the remote machine and asking them to stop the process. If you control the NFS server, you can unmount the filesystem temporarily (though this is disruptive): sudo umount -l /mnt/nfs/path/to/file (lazy unmount).
    • Why it works: NFS has specific mechanisms for handling file locks and open files across the network. If a file is opened for execution on a client, the server may prevent modifications to prevent data corruption or inconsistent states for the executing process.
  4. Core Dumps in Progress:

    • Diagnosis: A process crashed and the system is currently writing a core dump to a file (often named core or core.<pid>).
    • Command: sudo lsof /path/to/core/file or sudo lsof | grep core
    • Fix: Wait for the core dump generation to complete. This can take a while for large processes. If it’s stuck, you might need to find the process responsible for writing the core dump (often systemd-coredump or a similar service) and restart that service. sudo systemctl restart systemd-coredump.service.
    • Why it works: Writing a core dump involves writing a large amount of data to a file. The kernel prevents modifications to this file while it’s being written to ensure the integrity of the dump for debugging.
  5. Immutable Attribute Set on the File:

    • Diagnosis: The file has been marked as immutable, preventing any modifications, even by root. This is a security feature.
    • Command: sudo lsattr /path/to/your/file
    • Fix: Remove the immutable attribute: sudo chattr -i /path/to/your/file.
    • Why it works: The i attribute in lsattr means the file cannot be modified, deleted, or renamed. Removing this attribute allows standard file operations, including editing.
  6. Read-Only Filesystem:

    • Diagnosis: The entire filesystem containing the file is mounted as read-only.
    • Command: mount | grep " /path/to/filesystem " (e.g., mount | grep " / ") and look for ro in the options.
    • Fix: Remount the filesystem as read-write. For the root filesystem: sudo mount -o remount,rw /. For other filesystems, replace / with the appropriate mount point.
    • Why it works: A read-only mount explicitly forbids any writes to the filesystem. Remounting with rw (read-write) re-enables write operations.

The next error you’ll likely encounter is EPERM (Operation not permitted) if you try to modify a file that’s still locked, or a Permission denied error if you lack the necessary write permissions after the lock is released.

Want structured learning?

Take the full Linux & Systems Programming course →