-1

I have a situation where I am waiting for a file in a certain directory. So what I am doing here is as follows. (File is moving from some other directory)

if [ `find $SOURCE_DIR -name $FILE_PATTERN -type f -mmin +$FILE_STABILITY_TIME | wc -l` -ge 1 ]
then This
else
That

What happens when the file comes, it creates a file and then starts inserting data. Now I have a doubt in my mind that it may fail for the following situation.

The file is huge in gigs, it creates the file and starts writing in it. File size stays same for a time and then changes after some time and process is still writing data. My process just looks at timestamp and it will go to if or else based on condition.

So is there a way to make sure the file writing has been done? And the file I am looking in the folder is stable. (Complete)

agc
  • 7,973
  • 2
  • 29
  • 50
Deep
  • 342
  • 3
  • 12
  • Your code is not syntactically valid. The argument after `[` cannot be a command. Probably you want `if find ...` though comparing the number of output characters to zero is also an [antipattern](http://www.iki.fi/era/unix/award.html#wc). What exactly are you hoping that this code should actually do? – tripleee Jun 06 '19 at 18:18
  • Can you get any cooperation from the side writing the file? For example, can you make it `flock` the file? – John Bollinger Jun 06 '19 at 18:24
  • @Tripleee, Formatting of the code messed the syntax. I updated the Syntax. Have a look. – Deep Jun 06 '19 at 19:03
  • Getting a sane exit code out of `find` to indicate whether or not it found a file is harder than it should be; but simply `if find ... | grep -q .` should be significantly more succinct, robust, and idiomatic than the `wc` antipattern. (Also, it looks like you still have the closing backtick in the wrong place.) – tripleee Jun 06 '19 at 19:07

1 Answers1

4

perhaps use fuser <path> to determine if the file is still open by a process. However, this wouldn't prevent a writer from re-opening the file at some point and appending to it.

jspcal
  • 50,847
  • 7
  • 72
  • 76
  • The file is being written by some other tool (Perl is writing and it is an independent process), still, Shell will detect that it is still open? – Deep Jun 06 '19 at 18:14
  • That's what `fuser` does, yes. (Not the shell; `fuser`.) – tripleee Jun 06 '19 at 18:47