在后台运行长脚本 [英] Running long script in the background

查看:66
本文介绍了在后台运行长脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在尝试编写一个通过ssh调用脚本的python cgi,

问题是脚本需要很长时间如此执行Apache

使CGI超时,我从未看到任何输出。脚本设置为

每隔3秒向stdout打印一次进度报告,但在子进程被杀之前我从未看到任何输出
>
这就是我在python脚本中的内容:


command =" ssh -l root%s / scripts / xen / xen-create-win- vps1.sh%s" %

(主机,domuname)

输出= os.popen(命令)

输出线:

print line.strip()


这是bash脚本的副本。

http://watters.ws/script.txt


我也尝试过使用os.spawnv来在后台运行ssh并且没有任何东西

发生。


有没有人知道实时输出显示的方法?

Hello,

I am trying to write a python cgi that calls a script over ssh, the
problem is the script takes a very long time to execute so Apache
makes the CGI time out and I never see any output. The script is set
to print a progress report to stdout every 3 seconds but I never see
any output until the child process is killed.

Here''s what I have in my python script:

command = "ssh -l root %s /scripts/xen/xen-create-win-vps1.sh %s" %
(host, domuname)
output = os.popen(command)
for line in output:
print line.strip()

Here''s a copy of the bash script.

http://watters.ws/script.txt

I also tried using os.spawnv to run ssh in the background and nothing
happens.

Does anybody know a way to make output show in real time?

推荐答案

有没有人知道实时输出的方法?


你可以把:#!/ usr / bin / python -u

在脚本顶部有无缓冲的二进制stdout和stderr。

Does anybody know a way to make output show in real time?

You can put: #!/usr/bin/python -u
at the top of the script to have unbuffered binary stdout and stderr.


wa ******* @ gmail.com 写道:
wa*******@gmail.com wrote:

您好,


我正在尝试编写一个python cgi,通过ssh调用脚本,

问题是脚本需要很长时间才能执行,因此Apache

使CGI超时,我从未看到任何输出。脚本设置为

每隔3秒向stdout打印一次进度报告,但在子进程被杀之前我从未看到任何输出
>
这就是我在python脚本中的内容:


command =" ssh -l root%s / scripts / xen / xen-create-win- vps1.sh%s" %

(主机,domuname)

输出= os.popen(命令)

输出线:

print line.strip()
Hello,

I am trying to write a python cgi that calls a script over ssh, the
problem is the script takes a very long time to execute so Apache
makes the CGI time out and I never see any output. The script is set
to print a progress report to stdout every 3 seconds but I never see
any output until the child process is killed.

Here''s what I have in my python script:

command = "ssh -l root %s /scripts/xen/xen-create-win-vps1.sh %s" %
(host, domuname)
output = os.popen(command)
for line in output:
print line.strip()



每次打印后尝试sys.stdout.flush()。


或尝试一下像这样:

导入系统,时间


类FlushFile:

def __init __(self,fd):

self.fd = fd

def flush(self):

self.fd.flush()

def write(self ,str):

self.fd.write(str)

self.fd.flush()


oldstdout = sys .stdout

sys.stdout = FlushFile(sys.stdout)


for i in range(5):

print" ; Hello",

time.sleep(0.5)

打印


-

Thomas Güttler, http://www.thomas-guettler.de/ http://www.tbz-pariv.de/

电子邮件:guettli( *)thomas-guettler + de

Spam Catcher: ni * *************@thomas-guettler.de

try sys.stdout.flush() after every print.

Or try something like this:
import sys, time

class FlushFile:
def __init__(self, fd):
self.fd = fd
def flush(self):
self.fd.flush()
def write(self, str):
self.fd.write(str)
self.fd.flush()

oldstdout = sys.stdout
sys.stdout = FlushFile(sys.stdout)

for i in range(5):
print "Hello",
time.sleep(0.5)
print

--
Thomas Güttler, http://www.thomas-guettler.de/ http://www.tbz-pariv.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: ni**************@thomas-guettler.de


2月6日上午8:36 ,jasonmc < jasonmccandl ... @ gmail.comwrote:
On Feb 6, 8:36 am, "jasonmc" <jasonmccandl...@gmail.comwrote:

有没有人知道实时输出的方法?
Does anybody know a way to make output show in real time?



您可以在脚本顶部放置:#!/ usr / bin / python -u

以获得无缓冲的二进制标准输出和标准错误。


You can put: #!/usr/bin/python -u
at the top of the script to have unbuffered binary stdout and stderr.



谢谢。我试过了,但它仍然等待输出。


一切正常,直到我调用popen函数,然后它

冻结。我想要的是实时打印输出,就像我从shell运行它时所做的那样。


Thanks. I tried that but it still times out waiting for output.

Everything works fine until I call the popen function, then it
freezes. What I want is to print the output in real time, just like
it does when I run it from a shell.


这篇关于在后台运行长脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆