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
1
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.