Hotfolder with inotify-tools, loop FOR not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hotfolder with inotify-tools, loop FOR not working
# 1  
Old 04-05-2018
Hotfolder with inotify-tools, loop FOR not working

I can not understand why this little script with a loop processes only one file.

At boot in /etc/rc.local i wrote:

Code:
/usr/local/bin/./myscript &

This is myscript:

Code:
#!/bin/bash
    while inotifywait -e create /HOTFOLDER/ ; do

    for fullname in /HOTFOLDER/*.xlsx; do
 
    if ! [ -d "$fullname" ]; then
            send=/HOTFOLDER/$(date +'SEND-%F')
            sleep 10s
                    mkdir -p $send
                    mv -- "$fullname" $send
    fi

    done
    done


I have try also the option -m in inotifywait, but same problem:

Code:
 while inotifywait -m -e create /HOTFOLDER/ ; do

# 2  
Old 04-06-2018
I know nothing about inotifywait but it looks to me like you are using it wrong.
Try something like this:

Code:
inotifywait -m -e create /HOTFOLDER/ 2>/dev/null | while read folder event file
do
   if [[ "$event" =~ CREATE ]]
   then
      fullpath="$folder/$file"
     # process fullpath here
   fi
done

In bash everything inside that loop will be in a subshell and none of your variables will survive the loop.

Andrew

Edit: Just realised the redirect is not necessary:
Code:
inotifywait -m -q -e create /HOTFOLDER/ | while read folder event file
do
   if [[ "$event" =~ CREATE ]]
   then
      fullpath="$folder/$file"
     # process fullpath here
   fi
done


Last edited by apmcd47; 04-06-2018 at 05:38 AM.. Reason: Just realised the redirect is not necessary
# 3  
Old 04-06-2018
Quote:
Originally Posted by apmcd47
I know nothing about
[/CODE]In bash everything inside that loop will be in a subshell and none of your variables will survive the loop.

Andrew

Edit: Just realised the redirect is not necessary:
Code:
inotifywait -m -q -e create /HOTFOLDER/ | while read folder event file
do
   if [[ "$event" =~ CREATE ]]
   then
      fullpath="$folder/$file"
     # process fullpath here
   fi
done

Thnk you for your answer. after I will try your suggestion
the problem with myscript:
If in the HOTFOLDER there are 10 .xlsx files and then I copy a new one, the loop works all file to the end.
If there is no file inside the HOTFOLDER and then I copy 10.xlsx, the loop processes only one
# 4  
Old 04-06-2018
Not seeing any reason in the script for the behaviour you describe, I think we need more context info. Please post the output of the script run with the -x option set, and also a directory listing from within every loop.
# 5  
Old 04-06-2018
Quote:
Originally Posted by RudiC
Not seeing any reason in the script for the behaviour you describe, I think we need more context info. Please post the output of the script run with the -x option set, and also a directory listing from within every loop.
Thank you Rudic for the answer

I have copied in the HOTFOLDER 1.xlsx 2.xlsx 3.xlsx 4.xlsx
as you can see, only the 4.xlsx number was processed and the others remained in the HOTFOLDER

after that I copied the 4.xlsx number back into the HOTFOLDER and now it has processed all the xlsx files present.

I highlight you in bold inotifywait waiting to receive input

this is the output of $:bash -x myscript.sh

Code:
+ inotifywait  -e create /HOTFOLDER/
Setting up watches.
Watches established. (start of myscript)
+ for fullname in '/HOTFOLDER/*.xlsx'
+ '[' -d /HOTFOLDER/4.xlsx ']'
++ date +SEND-%F
+ send=/HOTFOLDER/SEND-2018-04-06
+ sleep 10s
+ mkdir -p /HOTFOLDER/SEND-2018-04-06
+ mv -- /HOTFOLDER/4.xlsx /HOTFOLDER/SEND-2018-04-06
+ inotifywait  -e create /HOTFOLDER/
Setting up watches.
Watches established. (after copy 1-2-3-4.xlsx, as you can see it only processes number 4)
+ for fullname in '/HOTFOLDER/*.xlsx'
+ '[' -d /HOTFOLDER/1.xlsx ']'
++ date +SEND-%F
+ send=/HOTFOLDER/SEND-2018-04-06
+ sleep 10s
+ mkdir -p /HOTFOLDER/SEND-2018-04-06
+ mv -- /HOTFOLDER/1.xlsx /HOTFOLDER/SEND-2018-04-06
+ for fullname in '/HOTFOLDER/*.xlsx'
+ '[' -d /HOTFOLDER/2.xlsx ']'
++ date +SEND-%F
+ send=/HOTFOLDER/SEND-2018-04-06
+ sleep 10s
+ mkdir -p /HOTFOLDER/SEND-2018-04-06
+ mv -- /HOTFOLDER/2.xlsx /HOTFOLDER/SEND-2018-04-06
+ for fullname in '/HOTFOLDER/*.xlsx'
+ '[' -d /HOTFOLDER/3.xlsx ']'
++ date +SEND-%F
+ send=/HOTFOLDER/SEND-2018-04-06
+ sleep 10s
+ mkdir -p /HOTFOLDER/SEND-2018-04-06
+ mv -- /HOTFOLDER/3.xlsx /HOTFOLDER/SEND-2018-04-06
+ for fullname in '/HOTFOLDER/*.xlsx'
+ '[' -d /HOTFOLDER/4.xlsx ']'
++ date +SEND-%F
+ send=/HOTFOLDER/SEND-2018-04-06
+ sleep 10s
+ mkdir -p /HOTFOLDER/SEND-2018-04-06
+ mv -- /HOTFOLDER/4.xlsx /HOTFOLDER/SEND-2018-04-06
+ inotifywait  -e create /HOTFOLDER/
Setting up watches.
Watches established. (after copy 4.xlsx again, as you can see it  processes all xlsx present in HOTFOLDER)

# 6  
Old 04-06-2018
Where is the directory listing as requested? Did you copy all the files in one go, or one by one? By the way, why don't you mv all .xlsx files in one go?
# 7  
Old 04-06-2018
Quote:
Originally Posted by pasaico
Thnk you for your answer. after I will try your suggestion
the problem with myscript:
If in the HOTFOLDER there are 10 .xlsx files and then I copy a new one, the loop works all file to the end.
If there is no file inside the HOTFOLDER and then I copy 10.xlsx, the loop processes only one
It is because of the way you were using inotifywait.
Code:
inotifywait -e create /some/dir

will wait for the create event in /some/dir, report that event and exit.

You copy 10 files into that directory and only the first is processed because the script is finished (or at least the wildcard *.xlsx is expanded) before the second file is added.

Code:
inotifywait -m -e create /some/dir | while read dir event file

will run the loop for every file added to the directory /some/dir and will process the files one-at-a-time.

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Tip: inotify cron

Dear members, moderators and others. While working on <insert project>, a need has surfaced to watch a directory, and when a file comes, to do appropriate action. So, i started writing some shell code, aware of linux inotify-tools package with inotifywait. Also, i'm seeing a lot of similar... (1 Reply)
Discussion started by: Peasant
1 Replies

2. Red Hat

Issues installing inotify-tools on RedHat Linux

I wish to install inotify-tools-3.20.1-2.4.x86_64.rpm on Linux and fire inotifywait command. Initially i was looking for "inotify-tools-3.xx.tar.gz" as instructed here http://jensd.be/248/linux/use-inotify-tools-on-centos-7-or-rhel-7-to-watch-files-and-directories-for-events I have... (9 Replies)
Discussion started by: mohtashims
9 Replies

3. Shell Programming and Scripting

For-loop not working

Hello all, I would like to unzip some files with a for-loop. Is there anyone who could tell me how I should do this - in a correct way? for file in $(ls); do echo gzip -d < $file | tar xf -; done The problem is the pipe - I believe. But how could I do it? I need it for the command... (4 Replies)
Discussion started by: API
4 Replies

4. UNIX for Advanced & Expert Users

Dynamically add paths to inotify

I have initiated an inotify process with --fromfile option and the file contain the paths to be monitored. /usr/local/maldetect/inotify/inotifywait -d -r -o /usr/local/maldetect/inotify/inotify_log --fromfile /usr/local/maldetect/sess/inotify.paths.28364 --exclude (^/var/tmp/mysql.sock)... (2 Replies)
Discussion started by: anil510
2 Replies

5. Shell Programming and Scripting

while loop not working

hi everyone i am facing a very strange problem . please help me why my while loop is not working. below is my code #!/bin/ksh file=RCMC_ABC_999_080924_210813.DAR echo ${file} value=001 count=10 echo "abc" echo ${count} value=$((${value} + 1 )) echo ${value} while ] do echo... (3 Replies)
Discussion started by: aishsimplesweet
3 Replies

6. SCO

inotify() in SCO UNIX

Hi I'm going to write a program in C language to watch activity on file system in SCO UNIX openserver 5.0.6. Apparently Linux provide an API called "inotify" , I'm wondering what's the SCO UNIX package for this ? (1 Reply)
Discussion started by: javad1_maroofi
1 Replies

7. Shell Programming and Scripting

While loop not working

here we go.. While loop is not working file.. It also invokes one more shell scripts for which parameters need to passed on. while read line do #### #### We want to have a logfile for each load #### PLog="${LogDir}/${Script}.log"; #### ... (5 Replies)
Discussion started by: premkumardr
5 Replies

8. Shell Programming and Scripting

For loop not working...! :(

Could some one help me on this... For loop is working...! for x in $i do for y in $j do z=`echo $y | awk '{print $2}'` if then FS=`/usr/bin/echo $y` echo $FS >>$Basic_location/out.csv fi done CPRT="Cpoyright @ BTOIDCIM" done (3 Replies)
Discussion started by: bullz26
3 Replies

9. Shell Programming and Scripting

if loop is not working

Hello i am trying to remove a line using an input file , but this depends upon user interaction here is the sample #!/bin/sh echo "Please enter whether you want to remove Profile" read value1 if ;then sed /movie/d temp.txt> temp3.txt else echo "Script didnot remove profile" fi ... (3 Replies)
Discussion started by: ranga27
3 Replies
Login or Register to Ask a Question