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?

2 Answers 2


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/the-above-script.sh"
  • I have tens of thousands files. Do you mean that I need to check every other file?
    – sanmai
    Sep 3, 2013 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, 2013 at 14:55
  • For the amount of files I have this approach will take more time than the actual sync.
    – sanmai
    Sep 10, 2013 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.

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.