title: Shell script to monitor incoming files every second skips a second
body: <p>I have a script that monitors a directory for new incoming .wav files. Every second a new .wav file is created a stored in a directory /my/path/to/file.wav. I am running a shell script using inotifywait to monitor the previously mentionned directory, every time a .wav file is added --&gt; it runs another script which will then create a .json every second with data of the .wav file stored.</p>
  <p>The problem being it sometimes either skips a second or runs the script twice in one second which leads to a second lost.</p>
  <p>when it runs twice a second :</p>
  <pre><code>file opened at payloadtime: 2022-05-11.11:54:22 
  JSON FILE UPDATED2022: 05:11 11:54:23.174
  
  file opened at payloadtime: 2022-05-11.11:54:22
  JSON FILE UPDATED2022: 05:11 11:54:24.130
  
  file opened at payloadtime: 2022-05-11.11:54:24
  JSON FILE UPDATED2022: 05:11 11:54:25.160
  </code></pre>
  <p>The payloadtime is the time at which the .wav file was saved, and the JSON FILE UPDATED is when the Json file is created.
  The code ran twice for the 22th second(it ran twice for the same .wav that was created; before a new .wav file was created), which means it did not run for the 23rd second. FYI the .wav file creator creates a .wav file every 1.000s seconds, no problem with that part of the code.</p>
  <p>When it skips a second :</p>
  <pre><code>file opened at payloadtime2022-05-11.12:08:17
  JSON FILE UPDATED2022:05:11 12:08:18.949
  
  file opened at payloadtime2022-05-11.12:08:19
  JSON FILE UPDATED2022:05:11 12:08:20.173
  
  file opened at payloadtime2022-05-11.12:08:20
  JSON FILE UPDATED2022:05:11 12:08:21.281
  </code></pre>
  <p>Imagine a .wav file is created at 17.000s, 18.000s, 19.000s...etc
  since one of the scripts starts at ~17.200s and finished at 18.949s, The following code started at ~19.100s, which means the 18th second was skipped.</p>
  <p>This is my shell script :</p>
  <pre><code>inotifywait -m /home/pi/Desktop/station7hz2021/textfiles/ -e create -e moved_to |
      while read dir action file; do
          ./Calculs.exe &amp; #My c++ code
      done
  </code></pre>
  <p>What I don't understand is that my shell script using &quot;&amp;&quot; should be runnning every time a .wav file is created, it should not run twice for the same file created or skip a second...
  The .wav file directory has a wav file every second and never skips a second nor does it create 2 .wav file each second.</p>
  <p>My guess is the inotifywait might be the source of error??
  Any suggestions ?</p>