Search code examples
pythonwindowscygwinsubprocessrsync

Running rsync from python subprocess in windows


I need to run rsync from Python 2.7 app in windows 7 x64 (using cwRsync 5.5.0).

Everything works fine from command line: set CWRSYNCHOME in env to cwrsync binaries and run following command

rsync.exe "/cygdrive/e/test" [email protected]:

But when trying to run same command as python subprocess:

process = subprocess.Popen(['rsync.exe', '/cygdrive/e/test', '[email protected]:'],
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           shell=True,
                           env={'CWRSYNCHOME': './bin'})

stdout, stderr = process.communicate()
print 'STDOUT:{}\nSTDERR:{}'.format(stdout, stderr)

I get following error in stderr:

rsync: pipe: Operation not permitted (1)
rsync error: error in IPC code (code 14) at pipe.c(59) [sender=3.1.2]

Here is verbose rsync stdout:

FILE_STRUCT_LEN=16, EXTRA_LEN=4
cmd=<NULL> machine=192.168.1.14 user=test1 path=.
cmd[0]=ssh cmd[1]=-l cmd[2]=test1 cmd[3]=192.168.1.14 cmd[4]=rsync cmd[5]=--server cmd[6]=-vvvvve.LsfxC cmd[7]=. cmd[8]=. 
opening connection using: ssh -l test1 192.168.1.14 rsync --server -vvvvve.LsfxC . .  (9 args)
[sender] _exit_cleanup(code=14, file=pipe.c, line=59): entered
[sender] _exit_cleanup(code=14, file=pipe.c, line=59): about to call exit(14)

Tryed set shell=False and pass command as single line (not cmd and args) - error stil repeats.

What am i doing wrong ?


Solution

  • To get it work, rsync needs to be runned under cygwin's shell:

    process = subprocess.Popen(['sh.exe', '-c',
                                'rsync /cygdrive/e/test [email protected]:'],
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               stdin=subprocess.PIPE,
                               env={'CWRSYNCHOME': '/bin/',
                                    'PATH': '/bin/'})
    

    It's working (there is no ssh athorization in example above).