Best command for running shell command

Discussion in 'Python' started by Donald Duck, Jul 11, 2006.

  1. Donald Duck

    Donald Duck Guest

    I'm a little bit confused about what is the best way to run a shell command,
    if I want to run a command like

    xxxxxx -a -b > yyyyyy

    where I'm not interested in the output, I only want to make sure that the
    command was executed OK. How should I invoke this (in a Unix/linux
    environment)?

    The command module seem to give the resulting output and the various popen
    commands seem to be similar.
     
    Donald Duck, Jul 11, 2006
    #1
    1. Advertising

  2. Donald Duck

    Roy Smith Guest

    In article <>,
    Donald Duck <> wrote:

    > I'm a little bit confused about what is the best way to run a shell command,
    > if I want to run a command like
    >
    > xxxxxx -a -b > yyyyyy
    >
    > where I'm not interested in the output, I only want to make sure that the
    > command was executed OK. How should I invoke this (in a Unix/linux
    > environment)?


    The most straight-forward way would be:

    import os
    status = os.system ("xxxxxx -a -b > yyyyyy")
    if status == 0:
    print "it worked"
    else:
    print "it failed"

    You might also want to look at the new (in 2.4) subprocess module.
     
    Roy Smith, Jul 11, 2006
    #2
    1. Advertising

  3. Yes, I highly recommend the subprocess module. subprocess.call() can
    do almost anything you want to do, and the options are all pretty
    intuitive Whenever I need to write quick scripts for myself, it's what
    I use.

    THN


    Roy Smith wrote:
    > In article <>,
    > Donald Duck <> wrote:
    >
    > > I'm a little bit confused about what is the best way to run a shell command,
    > > if I want to run a command like
    > >
    > > xxxxxx -a -b > yyyyyy
    > >
    > > where I'm not interested in the output, I only want to make sure that the
    > > command was executed OK. How should I invoke this (in a Unix/linux
    > > environment)?

    >
    > The most straight-forward way would be:
    >
    > import os
    > status = os.system ("xxxxxx -a -b > yyyyyy")
    > if status == 0:
    > print "it worked"
    > else:
    > print "it failed"
    >
    > You might also want to look at the new (in 2.4) subprocess module.
     
    Thomas Nelson, Jul 11, 2006
    #3
  4. Donald Duck

    iapain Guest

    > where I'm not interested in the output, I only want to make sure that the
    > command was executed OK. How should I invoke this (in a Unix/linux
    > environment)?


    Remember few things about executing program within python
    1. Create a subprocess or child process and execute it.
    2. You should use "Timeout stratagy" i.e your execution took more than
    provided time then timeout this process. In linux/unix you may use
    singnal alarm to implement it.

    Best!
     
    iapain, Jul 11, 2006
    #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. Replies:
    2
    Views:
    5,846
    Jonathan Bromley
    Feb 18, 2005
  2. Marcus Liddle
    Replies:
    2
    Views:
    483
  3. Sandman
    Replies:
    4
    Views:
    944
    aurora
    Feb 23, 2005
  4. Java and Swing
    Replies:
    4
    Views:
    666
    Oliver Wong
    Sep 11, 2006
  5. tekion
    Replies:
    3
    Views:
    1,032
    Sean DiZazzo
    Dec 24, 2009
Loading...

Share This Page