exception KeyboardInterrupt and os.system command

Discussion in 'Python' started by darren kirby, Nov 27, 2005.

  1. darren kirby

    darren kirby Guest

    Hello all.

    I have a python script here which is just a wrapper for 2 or more system
    commands. I would estimate the program spends at least 95.5% of 'real' time
    running the system commands.

    I want to trap the [crtl-c] key combo and exit (somewhat) gracefully if the
    user decides to abort the program. I am using os.system for the system call,
    and I have wrapped the entire main loop in a try: except KeyboardInterrupt
    statement to try to attain these ends.

    As it is though, if the program is currently in the system command, only that
    system command is terminated, and the next loop of my program starts.

    Is there a way to make this work? ie: terminate the entire script? Will popen
    do this? I don't really want to use popen because all I need is for the
    system command to run, and check the exit status. Also, popen will pooch the
    output of the system commands (which I want to be printed to the console)
    because the system commands (faad, mpg123, and oggenc) have buffered output
    which won't properly be displayed if I simply print each line of the file
    object returned by popen.

    I don't want to use subprocess because I can't expect my users to have 2.4
    installed...

    OS is Linux, if it matters.

    If you need to see the code it is here:
    http://badcomputer.org/unix/dir2ogg/dir2ogg.bot

    Although, this code is the program as it stands, not the code I am testing.

    Thanks,
    -d
    --
    darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
    "...the number of UNIX installations has grown to 10, with more expected..."
    - Dennis Ritchie and Ken Thompson, June 1972

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQBDij2swPD5Cr/3CJgRAnFMAKCUBJEsuuUI2kwYWmdXmEF4diDEjgCgwCsm
    hcaFHgOzSSWmKGCqPni0X/Y=
    =qMkk
    -----END PGP SIGNATURE-----
    darren kirby, Nov 27, 2005
    #1
    1. Advertising

  2. darren kirby wrote:
    > Hello all.
    >
    > I have a python script here which is just a wrapper for 2 or more system
    > commands. I would estimate the program spends at least 95.5% of 'real' time
    > running the system commands.
    >
    > I want to trap the [crtl-c] key combo and exit (somewhat) gracefully if the
    > user decides to abort the program. I am using os.system for the system call,
    > and I have wrapped the entire main loop in a try: except KeyboardInterrupt
    > statement to try to attain these ends.
    >
    > As it is though, if the program is currently in the system command, only that
    > system command is terminated, and the next loop of my program starts.
    >
    > Is there a way to make this work? ie: terminate the entire script? Will popen
    > do this? I don't really want to use popen because all I need is for the
    > system command to run, and check the exit status. Also, popen will pooch the
    > output of the system commands (which I want to be printed to the console)
    > because the system commands (faad, mpg123, and oggenc) have buffered output
    > which won't properly be displayed if I simply print each line of the file
    > object returned by popen.



    From "man system":

    """
    The system() function hands the argument string to the command inter-
    preter sh(1). The calling process waits for the shell to finish
    execut-
    ing the command, ignoring SIGINT and SIGQUIT, and blocking SIGCHLD.
    """

    So - I guess its popen2 or nothing :)

    Regards,

    Diez
    Diez B. Roggisch, Nov 27, 2005
    #2
    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. PantherSE
    Replies:
    0
    Views:
    335
    PantherSE
    May 16, 2005
  2. Replies:
    3
    Views:
    775
    Donn Cave
    Nov 28, 2005
  3. darren kirby
    Replies:
    1
    Views:
    327
  4. Tony Nelson

    Trace KeyboardInterrupt exception?

    Tony Nelson, Jun 13, 2006, in forum: Python
    Replies:
    4
    Views:
    496
    Tony Nelson
    Jun 15, 2006
  5. Michael Goerz
    Replies:
    3
    Views:
    410
    Gabriel Genellina
    Feb 4, 2008
Loading...

Share This Page