What's new

Modifying file with sed --> "Device or resource busy"

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

FreshJR

Very Senior Member
I have a file located in /jffs/ mounted ontop of a file in the routers read-only partition via this command.

Code:
mount -o bind /jffs/scripts/MY_FILE.asp /www/READ_ONLY_FILE.asp

Some approaches allow me to modify/overwrite this file. Other approaches do not.

For example:

I am always ABLE to overwrite "/jffs/scripts/MY_FILE.asp" with curl or pscp.

I cannot overwrite this file via the move "mv" command.
I cannot make changes to this file via sed.

The following two lines of code error out with "Device or resource busy"

Code:
sed -i 's/ORIGINAL TEXT/NEW TEXT/' '/jffs/scripts/MY_FILE.asp'
mv '/jffs/scripts/MY_NEW_FILE.asp' '/jffs/scripts/MY_FILE.asp'
--

I am trying to modify this file with sed. What can I do??
 
Do you need to modify it often, or just to initialize it? If it's just to initialize it, you could run 'umount /www/READ_ONLY_FILE.asp' before trying to edit and remount it afterwards. Otherwise perhaps you could have /jffs/scripts/MY_FILE.asp redirect to a page in var wwwext (cloudflare won't let me add slashes...).
 
I have a file located in /jffs/ mounted ontop of a file in the routers read-only partition via this command.

Code:
mount -o bind /jffs/scripts/MY_FILE.asp /www/READ_ONLY_FILE.asp

Some approaches allow me to modify/overwrite this file. Other approaches do not.

For example:

I am always ABLE to overwrite "/jffs/scripts/MY_FILE.asp" with curl or pscp.

I cannot overwrite this file via the move "mv" command.
I cannot make changes to this file via sed.

The following two lines of code error out with "Device or resource busy"

Code:
sed -i 's/ORIGINAL TEXT/NEW TEXT/' '/jffs/scripts/MY_FILE.asp'
mv '/jffs/scripts/MY_NEW_FILE.asp' '/jffs/scripts/MY_FILE.asp'
--

I am trying to modify this file with sed. What can I do??
Could you issue a service stop_httpd to bring down the WebUI and in theory allow you to edit the page?
 
I cannot overwrite this file via the move "mv" command.
I cannot make changes to this file via sed.
That make sense. But I think you have two issues here.

1. In both these cases you are not changing the original file you are trying to replace it with another one. Even with "sed -i" all that is happening is that the changes are written to a temporary file and then that file is used to replace the original. So you are in effect trying to delete the file that you have bind mounted.

2. The file is currently opened by another process which is stopping you from deleting it.

So the proper way to modify it would be to close any open file handles (maybe Jack's stop_httpd). Then unmount the file before editing it. Finally remount the file. If you don't unmount the file the system will continue to use the stale contents of the original file.
 
Can you "cp" over the file instead of "mv"?
Interesting idea. Unfortunately that still changes the file descriptor inode (at least on my router) so you end up with the same "stale" copy.

However, instead of using cp you could use "cat tempfile > realfile". That works but is effectively the same as the OP's curl and pscp examples.

I'm always nervous about changing files dynamically when other processes have them open. These processes often don't know that the file has changed and continue to read/write/update file locations that not only may have changed but may in fact be beyond the end of the "new" file. [Yes, this is the voice of experience :(]. So I'd still suggest that these "other processes" need to be forced to re-read the modified file if they don't automatically do that.
 
Last edited:
I'm always nervous about changing files dynamically when other processes have them open.
I often mount/bind asp files to a writeable location for debugging.....use nano to edit the file then refresh in the browser window.
 
I often mount/bind asp files to a writeable location for debugging.....use nano to edit the file then refresh in the browser window.
I thought that scenario might be OK, presumably because the file in question is not actually being held open. I guess it's only read when the browser refreshes the page? Which makes me wonder what process is generating the OP's "Device or resource busy" message.
 
I'm always nervous about changing files dynamically when other processes have them open.

It’s a webpage so its just used in a read only manner.

The user will be served a new one on refresh. Or worst case scenario next time http load it into memory or whatever it is doing with it.

I had no issues with stale versions being served vs the overwritten copy with pscp or curl.

Will try cp or cat.
 
I often mount/bind asp files to a writeable location for debugging.....use nano to edit the file then refresh in the browser window.

Same. Or, I will overwrite it with a file sent through scp from my development VM.
 
I had no issues with stale versions being served vs the overwritten copy with pscp or curl.
Same. Or, I will overwrite it with a file sent through scp from my development VM.
That's because the inode doesn't change when modifying the file, but it does when you delete it (which is what mv and cp does). pscp/scp don't work the same way as cp does on the router, it's more like "cat xxx > yyy" so the inode is unchanged.
 
Yes modifying the file in place without changing its inode works.

The next issue was that you can't read and write to the same file in the same line via redirection or you will end up with a blank file.

Working solutions I tried:

1) you can copy the file into a temp file, make your changes in the temp file, cat the temp file over the original file, then delete the temp file

or

2) you can
Code:
echo "$( cat /jffs/myfile | sed 's/ORIGINAL/REPLACEMENT/')"  >  /jffs/myfile

I have no idea what is happening internally with the above, but it works.

I think:
1) a subshell is created that stores the entirety of the desired output
2) the subshell is first filled with the original file + modifications
3) after that subshell is filled it echos the lines in the subshell one by one into /jffs/myfile.
 
Last edited:

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top