相关疑难解决方法(0)

从python运行程序,并在脚本被终止后继续运行

我试过这样的事情:

subprocess.Popen(['nohup', 'my_command'],
                 stdout=open('/dev/null', 'w'),
                 stderr=open('logfile.log', 'a'))
Run Code Online (Sandbox Code Playgroud)

如果父脚本正常退出,则此方法有效,但如果我终止脚本(Ctrl-C),则所有子进程也将被终止.有办法避免这种情况吗?

我关心的平台是OS X和Linux,使用的是Python 2.6 Python 2.7.

python subprocess nohup

37
推荐指数
3
解决办法
3万
查看次数

在Python中运行后台进程,不要等待

我的目标很简单:启动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 "&" …
Run Code Online (Sandbox Code Playgroud)

python subprocess popen background-process

10
推荐指数
2
解决办法
4万
查看次数

标签 统计

python ×2

subprocess ×2

background-process ×1

nohup ×1

popen ×1