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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .