I am trying to use inotifywait to monitor a folder:inotifywait -m -r /home/oshiro/Desktop/work_folderThe command works and if I create files in that folder, all seems to work correctly.While the folder is being monitored, if I delete it, I get the following output:/home/oshiro/Desktop/work_folder/ MOVE_SELF /home/oshiro/Desktop/work_folder/ OPEN,ISDIR /home/oshiro/Desktop/work_folder/ CLOSE_NOWRITE,CLOSE,ISDIR /home/oshiro/Desktop/work_folder/ MOVE_SELF /home/oshiro/Desktop/work_folder/ ATTRIB,ISDIR /home/oshiro/Desktop/work_folder/ OPEN,ISDIR /home/oshiro/Desktop/work_folder/ DELETE Untitled Document/home/oshiro/Desktop/work_folder/ DELETE Untitled Document 2/home/oshiro/Desktop/work_folder/ CLOSE_NOWRITE,CLOSE,ISDIR /home/oshiro/Desktop/work_folder/ DELETE_SELF If I then re-create that folder again, while the monitoring is still taking place, inotifywait doesn't seem to continue monitoring it, unless I run inotifywait -m -r /home/oshiro/Desktop/work_folder again.How do I get around this issue?  I basically want to monitor a USB stick which will be plugged in and removed many times during a day.  When it's unplugged and plugged back in, I think inotifywait will stop monitoring it, the same way the folder above was deleted and re-created where inotifywait wasn't able to continue monitoring it, unless I run the above command again, i.e. inotifywait -m -r /home/oshiro/Desktop/work_folderShould I be using something more appropriate for such tasks and not use inotifywait?  cron is not suitable for my needs, as I am not after time based actions, I am after event based actions.
inotifywait not working when folder is deleted and re-created
linux;mount;directory;monitoring;inotify
First off, if you delete a folder that inotifywait is watching, then, yes, it will stop watching it. The obvious way around that is simply to monitor the directory one level up (you could even create a directory to monitor especially and put your work_folder in there.However this won't work if you have a folder underneath which is unmounted/remounted rather than deleted/re-created, the two are very different processes. I have no idea if using something other than inotifywait is the best thing here since I have no idea what you are trying to to achieve by monitoring the directory. However perhaps the best thing to do is to set up a udev rule to call as script which mounts the USB stick and starts the inotifywait process when it is plugged in and another to stop it again when it is unplugged.You would put the udev rules in a .rules file in /etc/udev/rules.d` directory. The rules would look something like:ENV{ID_SERIAL}==dev_id_serial, ACTION==add, \  RUN+=/path/to/script add '%E{DEVNAME}'ENV{ID_SERIAL}==dev_id_serial, ACTION==remove, \  RUN+=/path/to/script remove '%E{DEVNAME}'Where ID_SERIAL for the device can be determined by:udevadm info --name=/path/to/device --query=propertywith the script something like:#!/bin/shpid_file=/var/run/script_name.pidout_file=/var/log/script_name.log# try to kill previous process even with add in case something# went wrong with last removeif [ $1 = add ] || [ $1 = remove ]; then  pid=$(cat $pid_file)  [ $(ps -p $pid -o comm=) = inotifywait ] && kill $pidfiif [ $1 = add ]; then  /bin/mount $2 /home/oshiro/Desktop/work_folder  /usr/bin/inotifywait -m -r /home/oshiro/Desktop/work_folder \    </dev/null >$out_file 2>&1 &  echo $! >$pid_filefiAlso, make sure that the mounting via the udev rule does not conflict with and other process which may try to automatically mount the disk when it is plugged in.