简体   繁体   English

在Python subprocess.Popen()中处理Shell重定向

[英]Handling Shell Redirection in Python subprocess.Popen()

I'm developing a Python (2.7) module which will allow users to either run their commands locally or submit a job to a load sharing facility (like LSF, but home grown). 我正在开发一个Python(2.7)模块,该模块将允许用户在本地运行命令或将作业提交到负载共享工具(例如LSF,但必须自行开发)。 In this situation I want the users to be able to submit their commands like so: 在这种情况下,我希望用户能够像这样提交他们的命令:

cmd = 'convert_something.tcl some_file >! output_file'

This makes the conversion of their scripts (Tcl, Perl, Python, C/C++) easier. 这使得他们的脚本(Tcl,Perl,Python,C / C ++)的转换更加容易。 (I foresee changing that in the future, but for now...) The subprocess.Popen() command looks like this: (我预见会在将来进行更改,但是现在...)subprocess.Popen()命令如下所示:

args = cmd.split()
p = subprocess.Popen(args, shell=True)

However, no matter how a play with the arguments to Popen, I cannot get redirection to work. 但是,无论如何使用Popen的参数,我都无法重定向到工作。 I'm currently working in the tcsh shell, but I've also tried bash. 我目前在tcsh shell中工作,但我也尝试过bash。 I've tried specifying the shell, like so: 我试过指定外壳,像这样:

p = subprocess.Popen(args, executable='/bin/bash', pre_exec=oc.setsid)

But it gives me an error from the executing program, which I think I expect to happen. 但这给了我正在执行的程序一个错误,我认为这是我希望发生的。 I've read and re-read the documentation and searched, but no luck. 我已经阅读并重新阅读了文档并进行了搜索,但是没有运气。

I must be missing something obvious, because I recall doing this before (and it worked). 我肯定缺少一些明显的东西,因为我记得以前做过(而且确实奏效了)。 Any suggestions would be appreciated. 任何建议,将不胜感激。

You should be able to submit command "as is", eg. 您应该能够“按原样”提交命令,例如。

cmd = './test.sh > /tmp/ttt.log'
p = subprocess.Popen(cmd, shell = True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM