I need to monitor if, for example, file /tmp/somefile123 was created after some events. I tried to use inotifywait but here is a problem:

# inotifywait -q -e create /tmp/somefile?*
Couldn't watch /tmp/somefile?*: No such file or directory

because there is exactly no such file, I want to know if it will be there!

How can I resolve this issue?

UPD: Maybe if I explain what I want to reach it will be more clear.

I need to write shell script (sh) with minimal CPU consumption, something like this:

if [ $(inotifywait -e create $SPECIFIC_FILE) ]; then
    blah-blah-blah some actions
# And then similarly monitor if this file was deleted and then do another actions

I expect that script will stop execution on inotifywait -e create $SPECIFIC_FILE till this $SPECIFIC_FILE will not created and it would be better then

while [ ! -f $SPECIFIC_FILE ]; do
    blah-blah-blah some actions
    sleep 1

By having inotifywait check on the parent directory:

/tmp$ inotifywait -e create -d -o /home/me/zz /tmp
/tmp$ touch z1
/tmp$ cat ~/zz
/tmp/ CREATE z1

You can also specify the time format for the event with the -timefmt option. Also, if you want to act immediately by executing some script file, for instance, you may use tail -f in the script file to monitor continuously the log file, here /home/me/zz, or you can create a named pipe file and have inotifywait write to it, while your script reads from it.

  • Maybe I didn't write clearly enough what I want to reach. I edited my question – HeroFromEarth Apr 18 '17 at 6:17
  • 2
    @HeroFromEarth I am sorry, but my answer above fits exactly what you stated in your OP and your update. Why do you believe it does not? – MariusMatutiae Apr 18 '17 at 6:38
  • I misunderstood your answer at first, sorry. Is making log file and reading it with tailf | grep 'pattern' will do less CPU consumption then while loop? – HeroFromEarth Apr 18 '17 at 9:28
  • Mark your answer as accepted because this is right, but question about "what better" still open :) – HeroFromEarth Apr 18 '17 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.