1

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?

17
  • 1
    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. Commented Aug 19, 2011 at 14:43
  • 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. Commented Aug 19, 2011 at 14:48
  • @tripleee that's what the shell and executable arguments he's talking about do Commented Aug 19, 2011 at 14:49
  • @agf yes but if he needs the same bash he can use shell=false and use bash -c '...' as the single command to run. Commented Aug 19, 2011 at 14:55
  • @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. Commented Aug 19, 2011 at 15:11

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

thank you, Mike. I was also thinking directly to to start vncserver but I couldn't find where vncserver file is and it's unknown how environment is extended after specifying vnc module.
@yart, In that case Popen(["bash", "-c", "module load vnc; vncserver :8080 -localhost"]) or whatever might be your best bet.
Mike, thank you. This command returns 127 exit code, but if I specify "-ci" instead of just "-c" it works. Why it works with interactive shell only?
@yart, it's possible that the code to define the module builtin is in bashrc rather than bash_profile/profile, so module was never defined in a non-interactive session. Personally, I usually have my personal ~/.bash_profile execute my ~/.bashrc.
0

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

1 Comment

module is likely the Environment Modules shell builtin, not an executable.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.