Skip to main content
 首页 » 编程设计

Python,使用Popen提交qsub作业

2024年11月24日12Terrylee

我必须编写一个 python 脚本来使用 qsub (SGE) 提交作业。

我要执行的命令是这样的形式:

qsub -b y /usr/bin/L2prod filein fileout

其中 L2prod 是编译程序(二进制文件,这是 -b y 选项的原因),filein/fileout 只是带有输入/输出文件名称的字符串。 如果我打开一个 shell 并键入上一行,一切都会顺利进行。

在我的 python 程序中我有:

...  
 args=['qsub -b y ', L2prod, filein, fileout]  
 log.info('executing: '+' '.join(map(str,args))) 
 process=subprocess.Popen(args,shell=True) 
... 
etc. 

日志文件中的输出是:

INFO:job_submit:executing: qsub -b y /usr/bin/L2prod /data/L1/20180414-222503_L1.txt /data/L2/20180414-222503_L2.txt

看日志似乎命令行是正确的,但是我得到了以下错误:

qsub: command required for a binary job

没有提交作业。

似乎在 .Popen() 方法中忽略了“-b y”选项。我究竟做错了什么?我认为 shell=True 选项可以解决这个问题。

请您参考如下方法:

您必须将每个标志作为列表的单独字符串元素提供。第一行不同。

# args=['qsub -b y ', L2prod, filein, fileout] 
args=['qsub', '-b', 'y', L2prod, filein, fileout]  
log.info('executing: '+' '.join(map(str,args))) 
process=subprocess.Popen(args,shell=False) 

为什么应该避免使用 shell = True 参数。

https://medium.com/python-pandemonium/a-trap-of-shell-true-in-the-subprocess-module-6db7fc66cdfd