Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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 ]
   cp /home/myfile /somewhere/else

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@ exec "/root/scripts/"
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

If you have ssh access to that machine you should try

ps auxc | grep rsync

It'll show you running rsync processes.

share|improve this answer

Your Answer


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.