我试过这样的事情:
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.
对于以下命令:
subprocess.call(shlex.split(
"""/usr/local/itms/bin/iTMSTransporter -m lookupMetadata
-apple_id %s -destination %s"""%(self.apple_id, self.destination))
Run Code Online (Sandbox Code Playgroud)
它将整个输出打印到终端窗口.我如何在这里抑制所有输出?我尝试过subprocess.call(shlex.split(<command> > /dev/null 2&1)),但它没有产生所需的结果.我怎么会这样做?
我需要date | grep -o -w '"+tz+"'' | wc -w在我的localhost上使用Python 运行命令 .我正在使用subprocess相同的模块,并使用check_output我需要捕获输出相同的方法.
但是它给我一个错误:
Traceback (most recent call last):
File "test.py", line 47, in <module>
check_timezone()
File "test.py", line 40, in check_timezone
count = subprocess.check_output(command)
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception-
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)
请帮助我哪里出错了.我是python的新手
我很难解析subprocess.Popen的参数.我正在尝试在我的Unix服务器上执行脚本.在shell提示符下运行时的脚本语法如下:
/usr/local/bin/script hostname = <hostname> -p LONGLIST.无论我如何尝试,脚本都不在subprocess.Popen中运行
"="之前和之后的空格是强制性的.
import subprocess
Out = subprocess.Popen(['/usr/local/bin/script', 'hostname = ', 'actual server name', '-p', 'LONGLIST'],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
以上不起作用.
当我使用shell = False时,我得到了 OSError: [Errno 8] Exec format error
在Python中,调用命令subprocess但不打扰其输出的最短和标准方法是什么.
我尝试过subprocess.call它似乎返回输出.我并不担心,我只需要静默运行程序而不会使输出混乱.
如果它有帮助,我正在打电话pdflatex,我的意图就是打电话给它.
pip测试套件使用子进程调用来运行集成测试.最近放置了PR,删除了一些旧的兼容性代码.特别是它b()用显式使用b""文字替换了一个函数.然而,这似乎打破了特定子进程调用永远挂起的地方.更糟糕的是,它只会永远挂在Python 3.3(可能只有Python 3.3.5)上,并且不能轻易地在Travis之外复制.
相关的拉动请求:
其他Pull请求也会出现类似的问题,但是在不同版本的Python和不同的测试用例中它们会失败.这些Pull请求是:
另一位用户今天在IRC上向我报告了一个类似的问题,他们说他们可以在Ubuntu 14.04上使用Python 3.3从死神(但不是在OSX上)本地再现它,而不仅仅是在特拉维斯,就像我迄今为止大部分都能做到的那样.他们给我发了一些重现的步骤:
$ git clone git@github.com:xavfernandez/pip.git
$ cd pip
$ git checkout debug_stuck
$ pip install pytest==2.5.2 scripttest==1.3 virtualenv==1.11.6 mock==1.0.1 pretend==1.0.8 setuptools==4.0
$ # The below should pass just fine
$ py.test -k test_env_vars_override_config_file -v -s
$ # Now edit pip/req/req_set.py and remove method remove_me_to_block or change its content to print('KO') or pass
$ # The below should hang forever
$ py.test -k test_env_vars_override_config_file …Run Code Online (Sandbox Code Playgroud) 我正在用python测试子进程管道.我知道我可以直接在python中执行下面的程序,但这不是重点.我只是想测试管道,所以我知道如何使用它.
我的系统是Linux Ubuntu 9.04,默认为python 2.6.
我从这个文档示例开始.
from subprocess import Popen, PIPE
p1 = Popen(["grep", "-v", "not"], stdout=PIPE)
p2 = Popen(["cut", "-c", "1-10"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
print output
Run Code Online (Sandbox Code Playgroud)
这样的作品,但由于p1的stdin不被重定向,我在终端类型的东西喂管.当我输入^D关闭stdin时,我得到了我想要的输出.
但是,我想使用python字符串变量将数据发送到管道.首先我尝试写stdin:
p1 = Popen(["grep", "-v", "not"], stdin=PIPE, stdout=PIPE)
p2 = Popen(["cut", "-c", "1-10"], stdin=p1.stdout, stdout=PIPE)
p1.stdin.write('test\n')
output = p2.communicate()[0] # blocks forever here
Run Code Online (Sandbox Code Playgroud)
没工作.我尝试p2.stdout.read()在最后一行使用,但它也阻止.我补充说p1.stdin.flush(),p1.stdin.close()但它也没有用.我然后我开始沟通:
p1 = Popen(["grep", "-v", "not"], stdin=PIPE, stdout=PIPE)
p2 = Popen(["cut", "-c", "1-10"], …Run Code Online (Sandbox Code Playgroud) 如何使用线程和子进程模块生成并行bash进程?当我启动线程时,第一个答案就在这里:如何在Python中使用线程?,bash进程按顺序而不是并行运行.
我有一个案例要在Python中执行以下shell命令并获取输出,
echo This_is_a_testing | grep -c test
Run Code Online (Sandbox Code Playgroud)
我可以使用这个python代码在python中执行上面的shell命令,
>>> import subprocess
>>> subprocess.check_output("echo This_is_a_testing | grep -c test", shell=True)
'1\n'
Run Code Online (Sandbox Code Playgroud)
但是,由于我不想使用"shell = True"选项,我尝试了以下python代码,
>>> import subprocess
>>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout)
>>> p1.stdout.close()
>>> p2.communicate()
(None, None)
Run Code Online (Sandbox Code Playgroud)
我想知道为什么输出是"无",因为我已经参考了网页上的描述:http://docs.python.org/library/subprocess.html#subprocess.PIPE
我错过了代码中的一些要点吗?有什么建议/想法吗?提前致谢.
我一直试图了解一段时间,现在subprocess.call和之间的区别是什么subprocess.run.我知道最后一个是Python 3.5上的新内容,两者都基于subprocess.Popen,但我还不能理解其中的差异.