我的目标很简单:启动 rsync 并且不要等待。
Debian 上的 Python 2.7.9
示例代码:
rsync_cmd = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1)
rsync_cmd2 = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3} &".format(remote_user, remote_server, file1, file1)
rsync_path = "/usr/bin/rsync"
rsync_args = shlex.split("-a -e 'ssh -i /home/mysuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1))
#subprocess.call(rsync_cmd, shell=True) # This isn't supposed to work but I tried it
#subprocess.Popen(rsync_cmd, shell=True) # This is supposed to be the solution but not for me
#subprocess.Popen(rsync_cmd2, shell=True) # Adding my own shell "&" to background it, still fails
#subprocess.Popen(rsync_cmd, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True) # This doesn't work
#subprocess.Popen(shlex.split(rsync_cmd)) # This doesn't work
#os.execv(rsync_path, rsync_args) # This doesn't work
#os.spawnv(os.P_NOWAIT, rsync_path, rsync_args) # This doesn't work
#os.system(rsync_cmd2) # This doesn't work
print "DONE"
(我注释掉执行命令只是因为我实际上将所有试验保留在代码中,以便我知道我做了什么和没有做什么。显然,我会使用正确的方式运行脚本行未注释。)
发生的事情是这样的......我可以在服务器上观看传输,当传输完成时,我会在屏幕上打印“完成”。
我希望发生的是在发出后立即打印“完成”rsync
命令并开始传输。
看起来非常简单。我遵循了其他帖子中概述的详细信息,例如this https://stackoverflow.com/questions/1196074/how-to-start-a-background-process-in-python一和this https://stackoverflow.com/questions/89228/calling-an-external-command-in-python/2251026#2251026一个,但有些东西阻止它为我工作。
提前致谢。
(我已经尝试了 StackExchange 中能找到的所有内容,但不觉得这是重复的,因为我仍然无法让它工作。我的设置有些不对劲,需要帮助。)