![]() |
bash checking for file write completion before acting?
I have a bash script that checks for contents in a folder every 15 seconds and then acts on it's contents. This works great for the average size file however on very large files it starts acting on the file before it's completely written. Is there a facility in bash shell to get a file complete signal or such?
here is trigger to launch a larger script. Code:
#!/bin/sh |
Hi,
I don't see how the while loop is ever going to be exited. So the 'acting' part is never going to be executed. I assume that the 'acting' on the file actually happens inside the while loop. I further assume that the file that is going to be acted upon is generated by $HOME/bin/hpgl.sh. If you want to be sure that the file has definitely been written out then one way would be to not have $HOME/bin/hpgl.sh run in the background. Is there any reason why you can't do it like $HOME/bin/hpgl.sh >/dev/null 2>&1 Also: JOBNAME="$(ls *ps)" That's probably not what you want. You already have the filename stored in $f. |
I would use 'stat -c %s' to check that the file's size is constant. Check it once, wait, check it again, if it's the same, it's done.
Also, more info would help, because I don't know what is going on in the script. |
script
The script is over 500 lines mostly dealing in totally unrelated calc's and such.
I didn't want to post unneeded data... It triggers and works great now and is constantly running every few seconds as stated , the .ps file gets generated by an outside app that saves it in the monitored folder. The stat command is exactly what I'm looking for Thanks again |
Stat will usually work, but is not bulletproof. gs often writes in bursts, and it's difficult to say if it's ready or just constructing a page heavy with graphics.
If you have the lsof package installed, lsof -t file-or-directory will output the PIDs of any processes that have the thing still opened. It does a lot of unnecessary work, so I don't recommend this either for this purpose. (It's excellent for checking if somebody has a configuration file open and so on, though.) The best approach is to use the inotifywait command from the inotify-tools package. You simply run the command in monitoring mode, and it tells you when something interesting happens to the files or directories specified. For example: Code:
#!/bin/bashYou can also use inotifywait in a one-shot mode, with or without a timeout. It's all quite well described in the inotifywait manpage. Just remember that these are notifications of events already happened. Something else may already have happened to the target since. Hope you find this useful, Nominal Animal |
You might use 'lsof' or 'fuser' to check if the file is being used before attempting to process it.
|
The only robust solution is for whatever is writing the file to write it with a special name, say foo.part, and rename it, say to foo, when writing is completed.
|
tried a few solutions
I tried a few solutions:
As suspected stat only reports the changed file size at write complete. Inotify wait won't monitor for one non-existent file.(can't monitor the whole folder due to other file activity and need *.ps *.pdf *.eps *.plt for files monitored, trouble) solved it by adding a few seconds to the wait! :) and recommending saving as pdf on very large files (these will be approx .1% of jobs anyway) pdf's save 20 times faster from inkscape than ps files if fills are involved. Thanks for all your help. |
Quote:
Code:
#!/bin/bashCode:
#!/bin/bash@catkin: Not true. Inotify is just as robust, and simpler to implement. It will notify reliably after the file has been closed for writing. Nominal Animal |
Quote:
|
works!
Works like dream even on inkscape ps files that take a minute to write!
Thanks VERY much Can someone help write a small python method for me maybe 30-50 lines? It involves writing and reading specific lines to and from a file which is beyond my meager experience :) |
| All times are GMT -5. The time now is 03:16 PM. |