0

I have the following loop:

while inotifywait -qq --event close_write "$filebs" "$filevp"; do
    do_something()
done

The problem is that, at certain times, two events are triggered one right after the other, so that while the code is do_something() the first event, the second event is triggered.

Now, I'm fine with processing both events, and I'm fine with processing just the second event. But I'm not fine with processing just the first event.

How can I process them both?

I guess I need to print the events in a buffer, for e.g. with inotifywatch, and read from that buffer?

robertspierre
  • 3,218
  • 2
  • 31
  • 46
  • How about running `do_something` in the background? – user1934428 Jan 24 '23 at 07:14
  • @user1934428 isn't that polling? – robertspierre Jan 24 '23 at 07:23
  • No. Polling means that you continuously check something. `ifnotifywait` waits, until the event occurs. Therefore, for each event, one `do_something` process will be created. Still there is a race condition: If two events occur within an intervall which is shorter than the creation of your `do_something` child process, one will be lost. I don't know _inotifywait_ well, and perhaps there is an option in it to queue the events. That's why I wrote my proposal only as comment, not as answer. – user1934428 Jan 24 '23 at 07:29
  • I don't understand what "running do_something in the background" means then? – robertspierre Jan 24 '23 at 07:35
  • 1
    You do a `do_something &`. The loop does not wait then for your process to end, but immediately enters `inotifywait` again. In theory, this can mean that you have a **lot** of `do_something` processes running in parallel, and depending on what they actually do, you may want to restrict the number of these processes even if this means that you will loose events. But this is a general problem when you run processes in parallel and not specific to your concrete problem at hand. – user1934428 Jan 24 '23 at 07:53
  • I see what you mean. If both events are going to be processed, it is important that the `do_something` of the second events starts when the `do_something` of the first event has finished. This is because they are going to output something and only the output of the last event matters. – robertspierre Jan 24 '23 at 07:57
  • In this case, you have to redesign `do_something` somehow. You can't have the cake **and** eat it too. For instance, instead of handling the even completely immediately, `do_something` could write information about the event into a log file, or a named pipe, or via a socket, and a different process picks up this information at its own speed and processes it. But since `inotifywait` already has the builtin ability to log the events, you don't necessarily have to do your own logging, but could use the log created by _inotifywait_. – user1934428 Jan 24 '23 at 08:06

1 Answers1

0

I could be wrong but I think this will run on every event because I think that in your example you are restarting inotifywait after every event? -m, --monitor Instead of exiting after receiving a single event, execute indefinitely. The default behaviour is to exit after the first event occurs.

inotifywait -m -qq --format '%f' -e close_write "$filebs" "$filevp" | while read file
do


 echo "$file"
done