开发者

subprocess and extra args

开发者 https://www.devze.com 2023-03-25 11:17 出处:网络
I\'m trying to use the following code: 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\'.spli

I'm trying to use the following code:

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)

However, the result I recieve is:

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

Without the LD_LIBRARY_PATH part, it works fine. However, I need it. Thanks for the assistance.


Try adding shell = True to the Popen call:

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

The syntax you're using to set LD_LIBRARY_PATH is a shell syntax, so it's necessary to execute the command through the shell.


Here's a method that avoids using the 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)
0

精彩评论

暂无评论...
验证码 换一张
取 消