0

I am trying to monitor file creation with inotifywait and execute a long script on every file creation. Everything works smoothly when the action to be taken is on the same server. But when it requires doing something on a remote server, the action runs once even 2 files are created in the monitored directory.

This works even if two or more files are created. :

d="path/dir/path/"
    inotifywait -m  -e  create --format '%f' "${d}" | while read FILENAME
    do
        
        IFS='-' read -r -a action <<< $FILENAME
        if  [ ${action[0]} = "dothing" ]
    
        then 
    
                rsync -r -v /var/www/site1.com/sub/ /var/www/site2.com/sub/
                     
        fi 
    done

This does not work (involves interaction with another server). It executes once even if 2 or more files are created

d="path/dir/path/"
inotifywait -m  -e  create --format '%f' "${d}" | while read FILENAME
do
    
    IFS='-' read -r -a action <<< $FILENAME
    if  [ ${action[0]} = "dothing" ]

    then 

            ssh root@server2ip   "rsync -r -v root@server1ip:/var/www/site1.com/sub/ /var/www/site2.com/sub/"
                 
    fi 
done
yazzou
  • 45
  • 1
  • 5
  • 1
    Does this answer your question? [Using while loop to ssh to multiple servers](https://unix.stackexchange.com/questions/107800/using-while-loop-to-ssh-to-multiple-servers) – dhag Feb 19 '21 at 05:07

1 Answers1

0

I have added 0<&- at the end of my command and it works

d="path/dir/path/"
inotifywait -m  -e  create --format '%f' "${d}" | while read FILENAME
do
    
    IFS='-' read -r -a action <<< $FILENAME
    if  [ ${action[0]} = "dothing" ]

    then 

            ssh root@server2ip   "rsync -r -v root@server1ip:/var/www/site1.com/sub/ /var/www/site2.com/sub/" 0<&-
                 
    fi 

done

yazzou
  • 45
  • 1
  • 5