Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a remote system, over which I don't have any control, uploading a lot of files over rsync/ssh from time to time. After that I need to process the files. But I can't touch incomplete copy. How do I detect if there's an rsync session ongoing and bail out?

share|improve this question

1 Answer 1

You can check with lsof before copying the file to see if some program has it open, example:

[root@pr467958 ~]# lsof /var/lib/mysql/mysql.sock COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 23262 mysql 12u unix 0xffff880fd8573600 0t0 15647622 /var/lib/mysql/mysql.sock mysqld 23262 mysql 13u unix 0xffff8800260bc900 0t0 18648027 /var/lib/mysql/mysql.sock

The output of lsof will be blank if no program has the file open so the problem is a quite simple scripting matter: if the output is blank copy the file if not leave it by now

lsofoutput=`lsof /home/myfile`
if [ ! $lsofoutput ]
then
   cp /home/myfile /somewhere/else
fi

Just one last thing. If you don't have control over that system but you have a ssh user/password then you do have control, just issue the above commands over ssh like this:

ssh root@8.8.8.8 exec "/root/scripts/the-above-script.sh"
share|improve this answer
    
I have tens of thousands files. Do you mean that I need to check every other file? –  sanmai Sep 3 '13 at 13:09
    
Of course that has to be done automatically. - You get the list of files by using ls or find and put it inside an array. - Loop the array and for every file location run the above code inside the loop. - If the output of lsof is null then you can copy the file, if not you leave it and continue with the next file. –  Daniel J. Sep 3 '13 at 14:55
    
For the amount of files I have this approach will take more time than the actual sync. –  sanmai Sep 10 '13 at 8:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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