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?
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
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: