Timeline for Why can’t I remove a file inside a while-read loop with a Bash script?
Current License: CC BY-SA 4.0
24 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| 9 hours ago | history | became hot network question | |||
| 12 hours ago | history | edited | Giacomo1968 | CC BY-SA 4.0 |
Adding extra info from the comments.
|
| 13 hours ago | history | edited | Richard Lawson | CC BY-SA 4.0 |
added 75 characters in body
|
| 14 hours ago | history | edited | Richard Lawson | CC BY-SA 4.0 |
added 4 characters in body
|
| 15 hours ago | comment | added | jsotola |
run fuser "${FILE_TO_DELETE}" instead of rm "${FILE_TO_DELETE}" ... does fuser report which program is using the file?
|
|
| 15 hours ago | comment | added | jsotola |
does /usr/local/bin/polling.sh open the file?
|
|
| 15 hours ago | answer | added | Richard Lawson | timeline score: 5 | |
| 17 hours ago | comment | added | Chester Gillon |
Maybe the NFS mount option lookupcache will help - see the Unix & Linux answer What would cause NFS file move/delete operations to fail?.
|
|
| 17 hours ago | comment | added | Richard Lawson | No, they are normal file names like DRDEX.DAT | |
| 17 hours ago | comment | added | Chester Gillon |
Do the problematic filenames start with .nfs? See the Unix & Linux NFS mount: Device or resource busy.
|
|
| 17 hours ago | comment | added | Richard Lawson | Not quite; the polling script will output the name of a file, e.g. /etc/hosts, and the loop will remove that file. | |
| 17 hours ago | comment | added | LPChip | I'm not a bash expert, but the way I interpret your script is that it does the following: it polls for changes, then does a while read loop to open the file and read the content of the file, and while the file is open and each line is read, you try to delete the file at the same time. Sounds to me like you should not even put a while read in there at all. | |
| 17 hours ago | comment | added | Kamil Maciorowski |
Side note: if you want to use read for this, get familiar with IFS= read -r.
|
|
| 17 hours ago | comment | added | Richard Lawson | I actually added a "sleep 5" command to the loop before doing the rm. I still get "device or resource busy". | |
| 17 hours ago | comment | added | LPChip | Given that it says device or resource busy, is it possible its busy right at the time inside that loop, but if you wait just a nanosecond and try again that it will work? Eg. can you put a delay in your loop and see if that fixes it? | |
| 17 hours ago | comment | added | Richard Lawson | 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. | |
| 17 hours ago | comment | added | Chester Gillon | What type of file system is /file/that/needs/deleting on? | |
| 17 hours ago | comment | added | Richard Lawson | I had also tried adding lsof /file/that/needs/deleting to the script. It shows that nothing is holding the file open. | |
| 17 hours ago | comment | added | Chester Gillon |
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.
|
|
| 17 hours ago | history | edited | Giacomo1968 | CC BY-SA 4.0 |
edited body
|
| 17 hours ago | answer | added | Giacomo1968 | timeline score: 2 | |
| 17 hours ago | history | edited | Giacomo1968 | CC BY-SA 4.0 |
deleted 7 characters in body; edited title
|
| S 17 hours ago | review | First questions | |||
| 16 hours ago | |||||
| S 17 hours ago | history | asked | Richard Lawson | CC BY-SA 4.0 |