1
votes

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?

3
Does module load vnc need to be run in the same shell / command as vncserver? Why do you need /bin/bash? /bin/sh is almost certainly a symlink to /bin/bash. - agf
You need a shell in order to execute multiple commands. You can try something like 'bash -c "module load vnc; vncserver :8080 -localhost"' but it's probably not really the right way to do it. - tripleee
@tripleee that's what the shell and executable arguments he's talking about do - agf
@agf yes but if he needs the same bash he can use shell=false and use bash -c '...' as the single command to run. - tripleee
@agf, yes, it should be run in the same shell. You are right, /bin/sh is symlink to bash. I think then it shouldn't be the difference. - yart

3 Answers

2
votes

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',....

0
votes

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())
0
votes

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'])