1

I am using inotifywait to monitor a folder by using below way.

inotifywait -m /tmp/test -e CLOSE_WRITE -e MOVED_FROM --format '%T' --timefmt '%s' -q|<action>

In here if any change in /tmp/test i want to do an action.

For example if deleted two files it fires MOVED_FROM event, but i want to call only an action once. If it again modify i wan to fire the same action.

I tried to get the time. And try to get a uniq . But it did not work.

Please let me know any one experience this.

Vipin
  • 175
  • Why are you trying to execute an action only once after more then one event occured? Grouping events based on time doesn't seem really robust as an approach. If you share your ultimate goal (what are you trying to do overall?) other users may help you figure out a better way. (By the way, inotifywait ... | uniq doesn't work because of buffering; you need to line-buffer uniq to let it print the time of events as long as they're piped to it). – fra-san Jul 30 '20 at 20:12
  • Ok i am monitoring a folder using a inotifywait. If any file got deleted , added or modified i need to trigger an email script. This is the goal. But i don't want to use a Loop. Here what is happening is it fire two e mails based on changes. Please let me know you want more details. – Vipin Jul 30 '20 at 20:16
  • Thank you. What is the criterion to "group" file events? Trigger an email for every distinct second in which a file event happens? Trigger an email if at least n seconds have passed since the last email was sent? Trigger an email every n file events? Also, a loop can be designed not to trigger an email for every iteration; would it be acceptable in that case? – fra-san Jul 30 '20 at 21:38
  • Thanks for the reply, Ok it is like any changes (create , delete , modify) happening in this folders irrespective for what file and folder it is an alerts go to departments. No i don't want to Loop. Is it not possible to identify just only a change? – Vipin Jul 30 '20 at 23:51
  • Uh. If you delete two files, the file system sees two events. I.e. rm foo bar unlinks foo and bar separately. I don't think there is a way for inotifywait to know whether you executed rm foo bar or rm foo followed by rm bar. – fra-san Jul 31 '20 at 00:06
  • https://unix.stackexchange.com/q/601145 – alecxs Jul 31 '20 at 02:52
  • Alecxs thanks for the link. But my question is not on basic syntax. A particular problem i am facing with inotifywait. I explained it in comments. – Vipin Jul 31 '20 at 05:01
  • Hi fra-san can you please give me a little example with inotifywait to skip loop which you explained above. – Vipin Jul 31 '20 at 05:43
  • "Skipping", or triggering an action for a group of events only (and not for every single event) would be based on some criterion. That's why I was asking. It would help if you could edit your Q and show an example of the exact commands you are executing (or plan to execute; e.g. "rm foo bar, then, after some time, touch baz") and what you expect the system to do (e.g. only perform "action" once for every command I run (i.e. once for rm foo bar and once for touch baz); or, only perform "action" if the current event is of a different type than the event that triggered the last "action"). – fra-san Jul 31 '20 at 11:51

0 Answers0