Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12395

Re: killing a script

From Nobody <nobody@nowhere.com>
Subject Re: killing a script
Date 2011-08-29 07:52 +0100
Message-Id <pan.2011.08.29.06.52.46.29000@nowhere.com>
Newsgroups comp.lang.python
References <22a0dae6-8a60-4e68-82a5-31a949bd69d5@y8g2000prd.googlegroups.com>
Organization Zen Internet

Show all headers | View raw


On Sun, 28 Aug 2011 18:15:56 -0700, Russ P. wrote:

> Is there a
> simple way to ensure that the first Control-C will kill the whole darn
> thing, i.e, the top-level script? Thanks.

You might try using subprocess.Popen() or subprocess.call() rather than
os.system().

os.system() calls the platform's system() function. On Unix, this
specifically ignores SIGINT and SIGQUIT for the duration of the call,
ensuring that Ctrl-C and Ctrl-\ only affect the child process and not the
parent.

subprocess.Popen() doesn't perform any implicit signal handling; it's
implemented in Python in terms of os.fork() and os.execvp[e](). It also
has a better interface (i.e. you get to directly control the argument list
passed to the child process, rather than having to construct a shell
command and hope that you got the quoting/escaping correct).

This may not suffice if any of the descendent processes are moved into
their own process group, as signals generated by the tty driver are sent
only to the foreground process group. However, this is unlikely to be an
issue for simple non-interactive programs (e.g. standard Unix "commands").

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-28 18:15 -0700
  Re: killing a script MRAB <python@mrabarnett.plus.com> - 2011-08-29 02:52 +0100
    Re: killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-28 19:41 -0700
      Re: killing a script Chris Angelico <rosuav@gmail.com> - 2011-08-29 12:51 +1000
        Re: killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-28 20:08 -0700
          Re: killing a script Chris Rebert <clp2@rebertia.com> - 2011-08-28 20:16 -0700
            Re: killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-28 20:22 -0700
            Re: killing a script "Russ P." <russ.paielli@gmail.com> - 2011-08-29 14:26 -0700
          Re: killing a script Thomas Jollans <t@jollybox.de> - 2011-08-29 09:49 +0200
          Re: killing a script Arnaud Delobelle <arnodel@gmail.com> - 2011-08-29 23:53 +0100
            Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-30 14:13 +1000
              Re: killing a script Hans Mulder <hansmu@xs4all.nl> - 2011-08-30 12:40 +0200
              Re: killing a script Cameron Simpson <cs@zip.com.au> - 2011-09-09 10:03 +1000
                Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-09 19:07 +1000
                Re: killing a script Hans Mulder <hansmu@xs4all.nl> - 2011-09-09 12:13 +0200
                Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-09 22:16 +1000
                Re: killing a script Cameron Simpson <cs@zip.com.au> - 2011-09-10 08:29 +1000
                Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-10 11:25 +1000
                Re: killing a script Chris Angelico <rosuav@gmail.com> - 2011-09-10 11:37 +1000
                Re: killing a script Cameron Simpson <cs@zip.com.au> - 2011-09-10 18:49 +1000
                Re: killing a script Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-11 15:08 +1000
                Re: killing a script Nobody <nobody@nowhere.com> - 2011-09-10 10:47 +0100
      Re: killing a script Paul Rubin <no.email@nospam.invalid> - 2011-08-28 19:53 -0700
  Re: killing a script Nobody <nobody@nowhere.com> - 2011-08-29 07:52 +0100

csiph-web