Resolve Device or Resource is Busy Error in Linux

Device or resource busy in Linux

1. Introduction

When working with Linux, you might encounter the error message “Device or Resource is Busy” while trying to unmount a filesystem, detach a storage device, or perform operations on files or directories. This error indicates that the target you’re trying to operate on is currently in use by the system or a process, preventing your intended action.

Our goal is to effectively resolve this error, ensuring that we can safely proceed with operations such as unmounting a filesystem, deleting a file, or detaching a storage device without causing system instability or data loss.

2. Understanding the Error

Before diving into solutions, let’s understand why this error occurs. Linux, being a multi-user, multitasking system, allows multiple processes to access files and devices simultaneously. When a device or file is being accessed (read or written to) by a process, Linux locks it to prevent data corruption. Attempting an operation that requires exclusive access to a locked resource triggers the “Device or Resource is Busy” error.

3. Identifying the Cause

To resolve the error, the first step is identifying which process is holding onto the device or file. We’ll use the lsof and fuser commands for this purpose.

3.1 Using lsof (List Open Files)

lsof is a powerful utility that lists information about files opened by processes. To find out which process is using a specific file or device, we can use:

Using +D is particularly useful when we suspect that a file causing the “Device or Resource is Busy” error might not be directly within the top level of a given directory but rather nested within its subdirectories. This option ensures that no stone is left unturned in our search for the open file causing the issue.

Example:

If we’re trying to unmount a USB drive mounted at /media/usb and encounter the error, we can run:

This output tells us that a bash process with PID 54321 is using the USB drive.

3.2 Using fuser

fuser identifies processes using files or sockets. To check which process is accessing a mount point or device, use:

Example:

Again, for the USB drive scenario:

Output:

This indicates the same bash process with PID 54321 is accessing the USB drive.

4. Resolving the Error

Once the offending process is identified, we have several options to resolve the issue:

4.1 Safely Closing the Process

If the process is a user application, you might simply close it from its GUI or terminate it gently using:

Replace PID with the actual process ID. For our example, it would be kill 54321.

4.2 Forcefully Killing the Process

If the process does not respond, use the -9 option to forcefully kill it:

Replace PID with the actual process ID. For our example, it would be kill -9 54321.

Always be careful while using the kill command because it may cause system instability or data loss if not used correctly.

4.3 Unmounting with the unmount Command

If the resource is a filesystem, you can try to unmount it using:

The -l option lazily unmounts the filesystem, detaching it as soon as it’s not busy anymore.

4.4 Automating with a Script

For recurring issues, we might consider writing a simple bash script that uses lsof or fuser to find and terminate the offending process before performing the desired operation. Below solution will help us to automate with the script.

5. Using lsof, awk and kill Together [One Liner]

In addition to the detailed approaches mentioned earlier, there’s an efficient one-liner command that combines the power of lsof, awk, and kill to swiftly resolve the “Device or Resource is Busy” error. This method is particularly useful when we need a quick resolution without manually identifying and terminating each process.

To harness this method, we utilize the lsof command to list open files in a directory, awk to filter out the process IDs, and kill to terminate these processes. Here’s the command:

Let’s break down this command for better understanding:

  1. lsof +D /path/to/directory: This part of the command uses lsof to list all open files within a directory and its subdirectories. Replace /path/to/directory with the actual path where the resource or device is busy.
  2. awk '{print $2}': awk processes the output of lsof, extracting the second column which contains the process IDs (PIDs).
  3. tail -n +2: This command skips the first line of the output, which is usually the header containing column titles like “COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME”.
  4. xargs -r kill -9: Finally, xargs takes the list of PIDs and passes them to kill -9, forcefully terminating each process. The -r option prevents kill from running if there are no inputs, avoiding unnecessary errors.

5.1 Caution and Consideration

While this one-liner is highly effective, it’s important to use it with caution. Forcefully killing processes with kill -9 can lead to data loss or instability, especially if the processes are critical to system or application functionality. Always ensure that the directory specified in the command is the one you intend to target, and consider the implications of abruptly stopping the identified processes.

6. Conclusion

The “Device or Resource is Busy” error in Linux signals that a file or device is under use by a process, hindering our intended operations. By leveraging tools like lsof and fuser, we can pinpoint and manage these processes, ensuring the smooth execution of our tasks. It’s imperative to approach such situations with caution to avert potential data loss or system instability, especially when forcibly ending processes or unmounting filesystems.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *