subprocess executing shell

Discussion in 'Python' started by Tim Arnold, Oct 21, 2009.

  1. Tim Arnold

    Tim Arnold Guest

    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
    Tim Arnold, Oct 21, 2009
    #1
    1. Advertising

  2. En Wed, 21 Oct 2009 12:24:37 -0300, Tim Arnold <>
    escribió:

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

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 22, 2009
    #2
    1. Advertising

  3. Tim Arnold

    Tim Arnold Guest

    "Gabriel Genellina" <> wrote in message
    news:...
    > En Wed, 21 Oct 2009 12:24:37 -0300, Tim Arnold <>
    > escribió:
    >
    >> 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.
    >
    > --
    > Gabriel Genellina


    Thanks, I see my mistake now. Arggg, I keep forgetting that one.
    thanks,
    --Tim Arnold
    Tim Arnold, Oct 22, 2009
    #3
  4. Tim Arnold

    Nobody Guest

    On Wed, 21 Oct 2009 11:24:37 -0400, Tim Arnold wrote:

    > 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.
    Nobody, Oct 22, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Brown
    Replies:
    0
    Views:
    464
    Tom Brown
    Sep 22, 2005
  2. Samuel A. Falvo II
    Replies:
    0
    Views:
    481
    Samuel A. Falvo II
    Oct 8, 2008
  3. Rick King
    Replies:
    1
    Views:
    255
    Martin v. Löwis
    Jul 26, 2009
  4. Peter Otten
    Replies:
    3
    Views:
    727
    Nobody
    Feb 15, 2010
  5. hiral
    Replies:
    2
    Views:
    595
    Jean-Michel Pichavant
    May 5, 2010
Loading...

Share This Page