简体   繁体   中英

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). 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. (I foresee changing that in the future, but for now...) The subprocess.Popen() command looks like this:

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. I'm currently working in the tcsh shell, but I've also tried 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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