Need help with os.system in linux

Discussion in 'Python' started by akshay bhat, Jan 17, 2009.

  1. akshay bhat

    akshay bhat Guest

    Hello
    i am calling a program using os.system in python on Linux.
    However in i found that program being executed and soon returned 256.
    but when i ran it using terminal i got proper results.
    Now in case of windows, python waits till the process is finished,
    Can you please tell me how to implement this in linux?
    akshay bhat, Jan 17, 2009
    #1
    1. Advertising

  2. akshay bhat

    excord80 Guest

    On Jan 16, 7:05 pm, akshay bhat <> wrote:
    > Hello
    > i am calling a program using os.system in python on Linux.
    > However in i found that program being executed and soon returned 256.
    > but when i ran it using terminal i got proper results.
    > Now in case of windows, python waits till the process is finished,
    > Can you please tell me how to implement this in linux?


    Maybe it runs faster in GNU/Linux? :)

    Either way, you probably want to post some minimal example code if you
    would like more help on this.
    excord80, Jan 17, 2009
    #2
    1. Advertising

  3. akshay bhat <> writes:

    > i am calling a program using os.system in python on Linux.
    > However in i found that program being executed and soon returned 256.
    > but when i ran it using terminal i got proper results.


    Under Linux (but unfortunately not generally under Unix), you can
    interpret return code from os.system() using os.WIFSIGNALED() or
    os.WIFEXITED(), and os.WEXITSTATUS() or os.WTERMSIG(). Something
    like this:

    status = os.system(commandline)
    if os.WIFSIGNALED(status):
    print "Command was killed by signal", os.WTERMSIG(status)
    else:
    print "Command exited with return status", os.WEXITSTATUS(status)

    (os.WIFSIGNALED() and os.WIFEXITED() are each others inverses,
    for this use case. In the above code, I could have used 'not
    os.WIFEXITED(status)' instead of 'os.WIFSIGNALED(status)' without
    changing its meaning.)

    If you do that for your program, you will see that the program
    you call are exiting with return status 1 (that's what a return
    value of 256 from os.system() happens to mean), i.e it is calling
    exit() with 1 as argument.

    *Why* the program you are calling does that, is impossible to
    tell from the information you have given us. You haven't even
    told us what command line you are passing to os.system(), so if
    you need more help, you must tell us more.


    May I also suggest that you use the subprocess module instead of
    os.system(). The subprocess module is a newer and better interface
    for running external commands. For example, you can avoid having
    to deal with quoting shell metacharacters, and interpreting the
    return values are easier.


    --
    Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
    "Life IS pain, highness. Anyone who tells ! bellman @ lysator.liu.se
    differently is selling something." ! Make Love -- Nicht Wahr!
    Thomas Bellman, Jan 17, 2009
    #3
    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. Tony
    Replies:
    0
    Views:
    588
  2. markus
    Replies:
    22
    Views:
    638
    Dances With Crows
    Sep 22, 2004
  3. Nagaraj
    Replies:
    1
    Views:
    835
    Lionel B
    Mar 1, 2007
  4. Paul Sijben
    Replies:
    4
    Views:
    279
  5. fmbright
    Replies:
    8
    Views:
    199
Loading...

Share This Page