在环境变量蟒蛇调用子 [英] Calling a subprocess in python with environmental variables
问题描述
我试图写一个python脚本自动扫描使用Plex的媒体扫描复杂的一个部分。要做到这一点,我必须运行扫描器作为用户运行复杂(在这种情况下,它是'plex的'),以及向它提供环境变量'LD_LIBRARY_PATH'。我同时使用subprocess.call和subprocess.Popen没什么区别尝试。在任何情况下,我没有得到任何输出。
这里是code我使用的:
I am trying to write a python script to automatically scan a section of plex using the Plex Media Scanner. To do so, I must run the scanner as the user running plex (in this case it is 'plex') as well as provide it with the environment variable 'LD_LIBRARY_PATH'. I've tried using both subprocess.call and subprocess.Popen with no difference. In either case, I am not getting any output. Here is the code I am using:
#!/usr/bin/python
import os
import subprocess
import shlex
env = os.environ.copy()
env['LD_LIBRARY_PATH'] = '/usr/lib/plexmediaserver'
s = "/bin/su - plex -c '/usr/lib/plexmediaserver/Plex\ Media\ Scanner -s -c 2'"
task = shlex.split(s)
exitCode = subprocess.call(task, env=env, shell=True)
现在我已经有一个工作版本,做什么,我想要它做的,但我不得不求助于使用包装bash脚本这样做。你可以看到下面的code:
Now I already have a working version that does what I want it to do but I had to resort to using a wrapper bash script to do so. You can see the code below:
#!/bin/sh
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver
/usr/lib/plexmediaserver/Plex\ Media\ Scanner $@
和脚本的相关线,将其称为:
And the relevant line of the script which calls it:
exitCode = subprocess.call("/bin/su - plex -c '/var/lib/deluge/delugeScripts/pms.sh -s -c 2'", shell=True)
感谢您的帮助。
推荐答案
由于jordanm在他的评论中指出的:
As jordanm noted in his comment:
在 - 苏使其成为一个登录shell这将重新初始化环境
the - in su makes it a login shell which re-initializes the environment.
这篇关于在环境变量蟒蛇调用子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!