3

I have a polling script that will occasionally pop out the name of a file to delete. The file to delete will always be located on an NFS-mounted filesystem.

So I wrote this Bash script:

#!/bin/bash
 
/usr/local/bin/polling.sh |
while read FILE_TO_DELETE;
do
   rm "${FILE_TO_DELETE}"
done

When this script is run, I get:

"cannot remove '/file/that/needs/deleting.txt': Device or resource busy".

If I run the rm command outside the while-read loop, it works just fine, so it's not a permission or open file issue.

Can anyone explain why the rm command doesn't work inside this loop but works fine outside of it?

FWIW, I looked at the file context before and after modifying the file, using ls -laZ, and they look the same. This is truly perplexing.

New contributor
Richard Lawson is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
14
  • As suggested by the Stack Overflow answer Automatically solve rm cannot remove path : device or resource busy error try running lsof +D /file/that/needs/deleting to see if that identifies what has the file open. Commented 15 hours ago
  • I had also tried adding lsof /file/that/needs/deleting to the script. It shows that nothing is holding the file open. Commented 15 hours ago
  • 2
    What type of file system is /file/that/needs/deleting on? Commented 15 hours ago
  • NFS-mounted remote file system. I also want to emphasize that the rm command will work if it is run outside of the while-read loop, so it's not a permissions issue. I can also delete the file just fine from the command line. Commented 15 hours ago
  • 2
    Do the problematic filenames start with .nfs? See the Unix & Linux NFS mount: Device or resource busy. Commented 15 hours ago

2 Answers 2

5

I found that if I modified the file (by, say, appending an empty line to the end of the file), I could then run the rm command from inside the loop. I don't know why that is; somehow saving it locally overwrites some remote file attribute that prevented deletion within my loop.

That led me to try using the shred command, which overwrites the file before deleting it. That worked a charm. So I'm leaving this solution up in case it helps someone else, even if I don't fully understand why it's necessary.

#!/bin/bash
 
/usr/local/bin/polling.sh |
while read FILE_TO_DELETE;
do
   shred -n 1 -u "${FILE_TO_DELETE}"
done
New contributor
Richard Lawson is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • This has got to be some kind of crazy NFS-specific thing... Commented 7 hours ago
1

I don’t know why this is happening, but it could be related to file ownership and/or permissions.

So try to force the delete with the -f parameter like this:

rm -f "${FILE_TO_DELETE}"

If that doesn’t work, try changing the rm to ls -la to see if you can glean any insight on what might be happening:

ls -la "${FILE_TO_DELETE}"

Or even use the Z parameter to check for SELinux context with:

ls -laZ "${FILE_TO_DELETE}"
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.