We help IT Professionals succeed at work.
Get Started

alternative to inotifywait util for reliable script to watch file create event on Linux

165 Views
Last Modified: 2020-07-12
HI,
Have implemented inotify to watch a file creation and execute a custom bash script to process something. All works fine on a linux server. e. g EC2

Below is the scenario what I have tried so and work perfectly.
1. inotifywait will watch for a file creation on /tmp/source
2. As soon as I place a file. inotify picks it up and execute further script.

Now in below scenario inotify fails to identify the new file (CREATE event)
1. inotifywait will watch for a file creation on common NAS storage mounted as /mnt/nas_storage/input on an EC2
2. inotify is not identifying the new file placed by exteranl process like Control-M or User mapped the same NAS drive /mnt/nas_storage/input to their windows PC and copy/paste the file.
3. however, if I login to EC2 move the file from /mnt/nas_storage/input to /tmp/ and then move back to /mnt/nas_storage/input from /tmp. inotify picks up the file

sort of gues, OS is not taking the CREATE event when external process place a file

So i'm thinking to have a reliable file monitor logic. May be I should introduce shell script which will monitor a directory, sleep, and the loop.

could you help with reliable code snippet for me to try please


Existing Script for your quick reference
inotifyWrapper.sh
#!/bin/sh

echo "inotifyWrapper invoked..." >> ${cams_cloudwatch_log_file}
while true; do
  ./inotifyWatchFile.sh
done

Open in new window


inotifyWatchFile.sh
#!/bin/sh

echo "inotifywait is monitoring ${source_dir_path} for file ${source_filename}"
inotifywait -m -e create --format '%w%f' "${cams_source_dir_path}" | while read NEWFILE
do
  echo "$NEWFILE"
done

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE