子进程和额外参数

2024-04-18 12:03:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用以下代码:

args = 'LD_LIBRARY_PATH=/opt/java/jre/lib/i386/:/opt/java/jre/lib/amd64/ exec /opt/java/jre/bin/java -Xincgc -Xmx1G -jar craftbukkit-0.0.1-SNAPSHOT.jar'.split()
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

然而,我得到的结果是:

Traceback (most recent call last):
File "launch.py", line 29, in <module>
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1228, in _execute_child raise child_exception
OSError: [Errno 2] No such file or directory

如果没有LD_LIBRARY_PATH部分,它可以正常工作。但是,我需要它。谢谢你的帮助。


Tags: pathinpyliblinelibraryargsjava
2条回答

下面是避免使用shell的方法:

from subprocess import Popen
from os import environ

env = dict(os.environ)
env['LD_LIBRARY_PATH'] = '/opt/java/jre/lib/i386/:/opt/java/jre/lib/amd64/'
args = ['/opt/java/jre/bin/java', '-Xincgc', '-Xmx1G', '-jar', 'craftbukkit-0.0.1-SNAPSHOT.jar']
p = Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)

尝试将shell = True添加到Popen调用:

p = subprocess.Popen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

用于设置LD_LIBRARY_PATH的语法是shell语法,因此有必要通过shell执行命令。

相关问题 更多 >