How to Fix ‘Text File Busy’ (ETXTBSY) Error in Linux

Automating CI/CD deployment pipelines, executing automated test suites, or compiling binaries in Linux only to encounter the error bash: ./app: Text file busy, fork/exec: text file busy, or system calls failing with errno 26 (ETXTBSY) brings software delivery workflows to an abrupt halt. In POSIX operating systems, the term “text file” does not refer to a human-readable ASCII document; historically in Unix architecture, the compiled executable machine code segment of a binary is designated as the “text segment”. To guarantee process stability and memory mapping integrity, the Linux kernel enforces a strict invariant: a binary currently mapped into memory and actively executing cannot be opened for writing, and conversely, a file currently open for writing cannot be executed via execve(). This technical guide explains how the Linux VFS (Virtual File System) manages inode execution locks and provides step-by-step methods to resolve ETXTBSY collisions.

Why Does the Kernel Raise ETXTBSY (Text File Busy)?

The Linux kernel raises ETXTBSY to prevent memory corruption and kernel panics:

  • Overwriting an Actively Running Executable: A build script, compiler, or deployment tool attempts to overwrite or write data to an executable file (using cp, mv, or redirection >) while an instance of that binary is running in memory.
  • Executing a File Currently Open for Writing: A deployment script writes an updated binary to disk and immediately invokes it before the writing process closes its file descriptor.
  • Container Mount Inode Conflicts: Docker or Kubernetes container runtimes attempting to execute a binary mounted from the host filesystem that is currently locked by a host editor or build daemon.
  • Background Daemons and Zombie Processes: A previous version of your server application crashed or detached into a zombie state, holding an open execution reference on the physical file inode.

Method 1: Identify and Terminate the Executing Binary

Locate the running process that holds the text segment lock on the executable.

  1. Open your Linux terminal.
  2. Use fuser to inspect which process ID (PID) is currently locking the target binary:
    sudo fuser -v ./app
  3. The output displays the command, PID, and access type (e indicates running executable):
                         USER        PID ACCESS COMMAND
    ./app:               root      18421 ...e.  app
  4. Alternatively, use lsof:
    sudo lsof /path/to/app
  5. Terminate the locking process gracefully:
    sudo kill -15 18421
  6. If the process refuses to stop, force kill it:
    sudo kill -9 18421
  7. Once the process exits, you can overwrite or compile the binary without encountering the busy error.

Method 2: Use Atomic Replacement (unlink / rm Before Writing)

Rather than overwriting an active binary in-place, unlinking the file creates a new inode that does not conflict with running processes.

  1. In Linux filesystems (ext4, XFS, Btrfs), a running process holds a reference to the underlying inode, not the directory name.
  2. If you run cp new_app app, Linux attempts to write to the existing inode, triggering ETXTBSY.
  3. Instead, delete or rename the old binary first:
    # Remove the old directory entry (the running process keeps the old inode alive until it exits)
    rm -f ./app
    
    # Copy the fresh binary into place
    cp ./new_app ./app
    chmod +x ./app
  4. Alternatively, use install which handles unlinking automatically:
    install -m 755 ./new_app ./app

Method 3: Ensure File Descriptors Are Closed in Scripts

If your script downloads or writes a binary and executes it immediately, ensure write handles are flushed.

  1. In automated shell or Python pipelines, writing a file and executing it in rapid succession can trigger a race condition where the write file descriptor is still open:
    # Problematic shell code:
    curl -o /tmp/script.sh https://example.com/script.sh &
    /tmp/script.sh   # Fails with text file busy if curl hasn't finished writing
  2. Ensure background writing processes complete synchronously before execution:
    curl -sSL -o /tmp/script.sh https://example.com/script.sh
    chmod +x /tmp/script.sh
    /tmp/script.sh
  3. In Python scripts, ensure explicit context managers (with open(...) as f:) are used so the descriptor closes before calling subprocess.run().

Method 4: Resolve Docker Container Volume Mount Locks

When running applications inside Docker containers with bind mounts, host locks can reflect inside the container.

  1. If a Docker container fails to start with fork/exec /bin/app: text file busy, check for running containers sharing the mount:
    docker ps
  2. Stop the running container:
    docker stop <container_id>
  3. Ensure no local IDE or text editor on the host has the file open in an exclusive write lock.
  4. Relaunch the container with a clean mount.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.