What the ETXTBSY Error Means
The ETXTBSY error (often displayed as Text file busy or Device or resource busy) occurs in Linux when you attempt to perform an operation on a file (delete, modify, rename), but the system detects that the file is currently in use by an active process. This is a kernel protection mechanism to prevent data corruption or inconsistent states.
Typical scenarios for its appearance:
- Attempting to delete or overwrite an executable file (
.exe, binary) that is currently running. - A file is open in a program (text editor, IDE, antivirus) with a write lock.
- The file is located on a network file system (NFS) and is locked by a remote process.
Symptom: commands like rm, mv, vim, or others terminate with the message Text file busy or Resource temporarily unavailable.
Causes
- Executable file is running
You are trying to delete or overwrite a binary file or script that is currently being executed by the kernel as a process. Linux does not allow modifying executable files while they are running. - File is open by another process with an exclusive lock
Some applications (e.g., databases, editors) may open files in a mode that prevents other processes from reading or writing to them. - Network File System (NFS) locks the file
When using NFS, a remote client or server may hold a lock on the file, leading to a localETXTBSYerror. - File is used as a shared library
Although shared libraries (.so) can usually be overwritten, in rare cases (e.g., when actively loaded into memory) the kernel may temporarily lock them. - Antivirus or monitoring system
Daemons likeclamdorauditdmay scan the file and temporarily lock it for analysis.
Solutions
Method 1: Use lsof to Find and Manually Terminate the Process
lsof (list open files) is a utility that shows which processes are using a specified file.
- Install
lsofif it's not present:# For Ubuntu/Debian sudo apt update && sudo apt install lsof # For CentOS/RHEL/Fedora sudo yum install lsof # or dnf install lsof - Find the process holding the file:
lsof /path/to/file
Example output:COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME vim 1234 user txt REG 8,1 10240 12345 /home/user/script.sh
Here,PID(Process ID) is1234. - Terminate the process:
kill 1234
If the process does not respond to a regular signal, use force termination:kill -9 1234 - Repeat the file operation (deletion, editing, etc.).
⚠️ Important: Do not terminate system processes (e.g.,
systemd,sshd) without understanding the consequences. Ensure the PID belongs to the application that is actually working with your file.
Method 2: Use fuser for Quick Termination
fuser is a more aggressive utility that can immediately terminate all processes using a file.
- Install
fuser(usually included in thepsmiscpackage):sudo apt install psmisc # Ubuntu/Debian sudo yum install psmisc # CentOS/RHEL - View processes (optional):
fuser -v /path/to/file - Automatically terminate processes:
sudo fuser -k /path/to/file
The-kflag sends aSIGKILLsignal to all processes using the file. For a softer approach, use-TERM(default):sudo fuser -TERM /path/to/file - Verify the file is released and repeat the operation.
Method 3: Working with NFS and Network File Systems
If the file is on an NFS share, local lsof/fuser commands may not see the remote process. Actions:
- On the client (where the error occurs) find the PID:
fuser -k /mnt/nfs/file - If that doesn't work, unmount and remount the NFS share:
sudo umount /mnt/nfs sudo mount -t nfs server:/export /mnt/nfs
Or force a remount (if supported):sudo mount -o remount /mnt/nfs - On the NFS server check if any process is holding the file:
lsof /export/file
And terminate it.
Method 4: Wait for the File to Be Released
Sometimes a file is locked briefly (e.g., during log writing or caching). If the process terminates on its own:
- Wait a few seconds and retry the operation.
- Use a wait loop (for scripts):
while lsof /path/to/file >/dev/null; do sleep 1; done echo "File released"
Method 5: System Reboot (Last Resort)
If none of the methods work and the file is critically important and cannot wait, reboot the computer. This will guaranteed release all resources.
sudo reboot
💡 Tip: After rebooting, check if the process locking the file starts automatically (via systemd, cron). Disable it if necessary.
Prevention
To avoid the ETXTBSY error in the future:
- Close files after use — don't leave them open in editors or other programs.
- Do not run the same executable file multiple times simultaneously if it is not designed for it (e.g., via
systemdorcron). - Use lock files in scripts and applications to coordinate access to resources.
- When updating binary files, stop related services first:
sudo systemctl stop service_name # Replace the file sudo systemctl start service_name - For NFS, configure mount options with
noac(no attribute caching) oractimeo=0if locks occur frequently.
Conclusion
The ETXTBSY error is a signal that a file is temporarily unavailable due to process activity. Using utilities like lsof and fuser, you can quickly identify and remove the lock. In most cases, one of the first two methods is sufficient. Remember safety: only terminate processes that are definitely not system-critical.