subprocess executing shell

T

Tim Arnold

Hi, I'm writing a script to capture a command on the commandline and run it
on a remote server.
I guess I don't understand subprocess because the code below exec's the
user's .cshrc file even though by default shell=False in the Popen call.

Here's the code. I put a line in my .cshrc file: echo 'testing' which
appears when I run this script on the remote host.
------------------------
import os,sys,subprocess,shlex

def main():
if action:
action.insert(0,'rsh my_remotehost')
p = subprocess.Popen(shlex.split(' '.join(action)))
p.wait()

if __name__ == '__main__':
action = sys.argv[1:] or list()
main()
------------------------

Since the shell is executing in the child process anyway, is the only
difference when using shell=True is that environment variables can be
expanded in the command to be executed?

thanks,
--Tim Arnold
 
G

Gabriel Genellina

Hi, I'm writing a script to capture a command on the commandline and run
it
on a remote server.
I guess I don't understand subprocess because the code below exec's the
user's .cshrc file even though by default shell=False in the Popen call.

Do you mean it execs the .cshrc file in your *local* system or the
*remote* one?
Popen controls what happens on the local system only.
action.insert(0,'rsh my_remotehost')
p = subprocess.Popen(shlex.split(' '.join(action)))
p.wait()

Since the shell is executing in the child process anyway, is the only
difference when using shell=True is that environment variables can be
expanded in the command to be executed?

Note that in this case, "the child process" is rsh on the local system.
Popen has no control over what happens once rsh starts.
 
T

Tim Arnold

Gabriel Genellina said:
Do you mean it execs the .cshrc file in your *local* system or the
*remote* one?
Popen controls what happens on the local system only.


Note that in this case, "the child process" is rsh on the local system.
Popen has no control over what happens once rsh starts.

Thanks, I see my mistake now. Arggg, I keep forgetting that one.
thanks,
--Tim Arnold
 
N

Nobody

Hi, I'm writing a script to capture a command on the commandline and run it
on a remote server.
I guess I don't understand subprocess because the code below exec's the
user's .cshrc file even though by default shell=False in the Popen call.

Using shell=True causes Popen to explicitly invoke the command via a
shell, i.e. "/bin/sh -c <command>" on Unix or "cmd /c <command>" on
Windows. If you use shell=False (the default), it uses fork()+exec() or
CreateProcess().

If the command which you are running ends up invoking a shell (as rsh
probably does), that's nothing to do with Popen.

In particular, if csh is being invoked, that isn't Popen's doing. It will
use either /bin/sh on Unix or %COMSPEC% (which is typically cmd.exe) on
Windows.
Since the shell is executing in the child process anyway, is the only
difference when using shell=True is that environment variables can be
expanded in the command to be executed?

The difference is that the shell gets involved, so you can use environment
variables, backticks, redirections, pipelines, shell-builtins etc. It also
means that you need to quote or escape spaces and shell metacharacters
within arguments if you want them to be treated literally.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top