Terminating python script easily

Discussion in 'Python' started by Balban, Oct 22, 2009.

  1. Balban

    Balban Guest

    Hi,

    I have a python build script that calls various commands, some using
    os.system().

    Often, if I want to terminate the script prematurely, I press ctrl-c,
    but I have to do this many times before I can kill the script for
    good. I was wondering is there a way that I define a signal handler
    and kill the whole thing at once with a single ctrl-c? Perhaps I
    should also call my other scripts with a method other than os.system
    () as well?

    Thank you,

    Bahadir
    Balban, Oct 22, 2009
    #1
    1. Advertising

  2. On Thu, Oct 22, 2009 at 8:46 AM, Balban <> wrote:
    > Hi,
    >
    > I have a python build script that calls various commands, some using
    > os.system().
    >
    > Often, if I want to terminate the script prematurely, I press ctrl-c,
    > but I have to do this many times before I can kill the script for
    > good. I was wondering is there a way that I define a signal handler
    > and kill the whole thing at once with a single ctrl-c? Perhaps I
    > should  also call my other scripts with a method other than os.system
    > () as well?
    >
    > Thank you,
    >
    > Bahadir
    > --

    In Python, ctrl-C raises a KeyboarInterrupt. You can just catch that
    and terminate everything in the except clause. If you're using 2.6,
    subprocess.Popen has a terminate method you can use to quit the other
    scripts if you use that instead of os.system.
    Benjamin Kaplan, Oct 22, 2009
    #2
    1. Advertising

  3. Balban wrote:
    > Hi,
    >
    > I have a python build script that calls various commands, some using
    > os.system().
    >
    > Often, if I want to terminate the script prematurely, I press ctrl-c,
    > but I have to do this many times before I can kill the script for
    > good. I was wondering is there a way that I define a signal handler
    > and kill the whole thing at once with a single ctrl-c? Perhaps I
    > should also call my other scripts with a method other than os.system
    > () as well?
    >
    > Thank you,
    >
    > Bahadir
    >

    you may want to use subprocess instead of os.system.
    On catching CTRL+C, you kill all the pid started with subprocess and
    exit the script smoothly.

    JM
    Jean-Michel Pichavant, Oct 22, 2009
    #3
  4. On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant
    <> wrote:
    > Balban wrote:
    >>
    >> Hi,
    >>
    >> I have a python build script that calls various commands, some using
    >> os.system().
    >>
    >> Often, if I want to terminate the script prematurely, I press ctrl-c,
    >> but I have to do this many times before I can kill the script for
    >> good. I was wondering is there a way that I define a signal handler
    >> and kill the whole thing at once with a single ctrl-c? Perhaps I
    >> should  also call my other scripts with a method other than os.system
    >> () as well?
    >>
    >> Thank you,
    >>
    >> Bahadir
    >>

    >
    > you may want to use subprocess instead of os.system.
    > On catching CTRL+C, you kill all the pid started with subprocess and exit
    > the script smoothly.
    >
    > JM
    >


    Hmm. OK, this is what I suspected I needed. So no explicit signal
    catching is required I guess.

    I will look into it, thanks.


    Bahadir
    Bahadir Balban, Oct 22, 2009
    #4
  5. En Thu, 22 Oct 2009 10:03:51 -0300, Bahadir Balban
    <> escribió:
    > On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant
    > <> wrote:
    >> Balban wrote:
    >>>
    >>> Often, if I want to terminate the script prematurely, I press ctrl-c,
    >>> but I have to do this many times before I can kill the script for
    >>> good. I was wondering is there a way that I define a signal handler
    >>> and kill the whole thing at once with a single ctrl-c?


    >> you may want to use subprocess instead of os.system.
    >> On catching CTRL+C, you kill all the pid started with subprocess and
    >> exit
    >> the script smoothly.

    >
    > Hmm. OK, this is what I suspected I needed. So no explicit signal
    > catching is required I guess.


    Note that KeyboardInterrupt (the exception generated by pressing ^C) may
    be catched (sometimes inadvertedly) if the code uses a bare 'except'
    clause, and then the program continues normally, effectively ignoring ^C.
    The most generic 'except' clause should be, normally:
    try: ...
    except Exception: ...

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 23, 2009
    #5
  6. En Thu, 22 Oct 2009 10:03:51 -0300, Bahadir Balban
    <> escribió:
    > On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant
    > <> wrote:
    >> Balban wrote:
    >>>
    >>> Often, if I want to terminate the script prematurely, I press ctrl-c,
    >>> but I have to do this many times before I can kill the script for
    >>> good. I was wondering is there a way that I define a signal handler
    >>> and kill the whole thing at once with a single ctrl-c?


    >> you may want to use subprocess instead of os.system.
    >> On catching CTRL+C, you kill all the pid started with subprocess and
    >> exit
    >> the script smoothly.

    >
    > Hmm. OK, this is what I suspected I needed. So no explicit signal
    > catching is required I guess.


    Note that KeyboardInterrupt (the exception generated by pressing ^C) may
    be catched (sometimes inadvertedly) if the code uses a bare 'except'
    clause, and then the program continues normally, effectively ignoring ^C.
    The most generic 'except' clause should be, normally:
    try: ...
    except Exception: ...

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 23, 2009
    #6
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    795
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    932
    Jeff Epler
    Apr 20, 2004
  3. akineko
    Replies:
    3
    Views:
    2,601
    Jesse Noller
    Jan 29, 2009
  4. Lloyd Zusman

    Erb: terminating a script?

    Lloyd Zusman, Mar 2, 2005, in forum: Ruby
    Replies:
    8
    Views:
    144
    Lloyd Zusman
    Mar 3, 2005
  5. Debo
    Replies:
    12
    Views:
    197
Loading...

Share This Page