Starting module shell command from python subprocess module

别说谁变了你拦得住时间么 提交于 2019-12-11 06:25:01

问题


I'm trying to run vnc server, but in order to do it first I need to run 'module load vnc'.

If I call which module in loaded bash shell then the command in not found is the PATH but in the same time it's available. It looks like the command is built-in.

In other words it looks like I need to execute two commands at once module load vnc;vncserver :8080 -localhost and I'm writing script to start it from python. I have tried different variants with subprocess.Popen like

subprocess.Popen('module load vnc;vncserver :8080 -localhost', shell=True) 

which returns 127 exit code or command not found.

subprocess.Popen('module load vnc;vncserver :8080 -localhost', shell=False)

showing

File <path>/subprocess.py line 621, in \__init__    
                                   errread, errwrite)
OSError: [Errno 2] No such file or directory.

If I specify shell=True, it executes from /bin/sh but I need it from /bin/bash.

Specifying executable='/bin/bash' doesn't help as it loads new bash shell but it starts as string but not as process, i.e. I see in ps list exactly the same command I would like to start.

Would you please advise how to do start this command from subprocess module? Is it possible to have it started with shell=False?


回答1:


Environment Modules usually just modifies a couple environment variables for you. It's usually possible to skip the module load whatever step altogether and just not depend on those modules. I recommend

subprocess.Popen(['/possibly/path/to/vncserver', ':8080', '-localhost'], 
                 env={'WHATEVER': 'you', 'MAY': 'need'})

instead of loading the module at all.

If you do insist on using this basic method, then you want to start bash yourself with Popen(['bash',....




回答2:


If you want to do it with shell=False, just split this into two Popen calls.

subprocess.check_call('module load vnc'.split())
subprocess.Popen('vncserver :8080 -localhost'.split())



回答3:


You can call module from a Python script. The module command is provided by the environment-modules software, which also provides a python.py initialization script.

Evaluating this script in a Python script enables the module python function. If environment-modules is installed in /usr/share/Modules, you can find this script at /usr/share/Modules/init/python.py.

Following code enables module python function:

import os
exec(open('/usr/share/Modules/init/python.py').read())

Thereafter you can load your module and start your application:

module('load', 'vnc')
subprocess.Popen(['vncserver', ':8080', '-localhost'])


来源:https://stackoverflow.com/questions/7123181/starting-module-shell-command-from-python-subprocess-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!