1

So, in looking for a way to find out when a file is done being created to start the copy, I found inotifywait, but it doesn't seem to be working for me.

I'm using Redline to dump a remote computer's memory, and have the script so that it can find the right directory and file, but when I try to set up inotifywait, it just hangs and never returns that file is complete (I'm wondering if the file is held open by Redline until it's done with the whole scan, even though the actual memory image is done?)

I've tried

inotifywait -m -e create fileName

and

inotifywait -m -e close_write fileName

But neither seem to be working. They say watches created, but then just hang there long after the file isn't being written to anymore. And I don't know how to check to see if the remote machine still has a handle open on the file... cp certainly still works, and tail -f shows clearly that the file isn't being written to. Is there an elegant solution to notifying my script of the file being ready to copy over?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Arvandor
  • 181
  • 1
  • 5
  • 15
  • See if the process creating the file is still running, and if it is, see what file handles it still has open (lsof or, if on linux, check /proc/pid/fd as root). – finite graygreen May 07 '15 at 19:20
  • Being that it's happening on a remote windows machine whose share I've simply mounted to my linux box, I'm not sure this is possible? Maybe I'm wrong... – Arvandor May 07 '15 at 20:38
  • Ok, that changes **a lot**. `inotify` is a linux kernel feature, or more precisely a file system feature, on which the tool `inotifywait` relies. If the mounted windows share does not forward the equivalent `inotify` events windows (presumably) generates, then nothing will arrive to act on. You probably need a busy loop which checks for changes every X seconds by looking at the file size change. – finite graygreen May 07 '15 at 20:47
  • See http://superuser.com/questions/592546/watching-a-samba-share-for-modifications-from-linux or http://stackoverflow.com/questions/8124617/getting-file-create-notifications-for-cifs-mount-in-linux for the inotify/smb/cifs problem – finite graygreen May 07 '15 at 20:50
  • Guess I'll just have to add like a 3 minute sleep and hope that does the trick... – Arvandor May 07 '15 at 21:42

1 Answers1

0

You asked it to never exit:

   -m, --monitor
          Instead of exiting after receiving a single event, execute
          indefinitely.  The default behaviour is to exit after the
          first event occurs.

It waits indefinitely, try inotifywait -e close_write fileName

An alternative approach would be to implement a busy loop with --timeout / -t, but that assumes continuous data input:

while inotifywait -e modify -t 15 fileName; do
    echo "something was written in the last 15 seconds"
done
echo "no activity for 15 seconds. done?"
  • That doesn't seem to work... I get a `cp: cannot open 'filepath' for reading: Device or resource busy, after the 15 seconds are up, it never echo's that something was written. From watching the file with repeated ls -lha's, it looks like it goes from 0 bytes to 4.5 gigs instantaneously... So... I dunno? Is there a way I could leverage that cp error to have it try again in 5-10 seconds until it works? Or is that a bad way to handle it? Edit: Also, the inotifywait -e close_write version with no -m switch still just hangs and never stops its watch... – Arvandor May 07 '15 at 20:30