killing a script

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Sep 9 11:07:24 CEST 2011


Cameron Simpson wrote:

> On 30Aug2011 14:13, Steven D'Aprano <steve+comp.lang.python at pearwood.info>
> wrote:
> | On Tue, 30 Aug 2011 08:53 am Arnaud Delobelle wrote:
> | >> Yes, but if I am not mistaken, that will require me to put a line or
> | >> two after each os.system call. That's almost like whack-a-mole at the
> | >> code level rather than the Control-C level. OK, not a huge deal for
> | >> one script, but I was hoping for something simpler. I was hoping I
> | >> could put one line at the top of the script and be done with it.
> | > 
> | > Write a function!  That's what they're for after all :)
> | 
> | I'm not sure that this is actually as simple as that, especially using
> | os.system.
> | 
> | As I understand it, the scenario is this:
> | 
> | The main script looks something like this:
> | 
> | for x in whatever:
> |     os.system('something.py x')
> | 
> | Each time through the loop, a new Python process is started. Each
> | process runs in the foreground, capturing standard input, and so hitting
> | Ctrl-C kills *that* process, not the main script. Unless, by chance, the
> | Ctrl-C happens after the system call returns, but before the next one
> | starts, it is completely invisible to the parent process (the main
> | script). Wrapping os.system in a function does nothing to fix that.
> 
> Presuming you're talking about UNIX, this is not correct.
> 
> Ctrl-C at the terminal delivers SIGINT to _every_ process in the
> controlling process group for the terminal. It also has _nothing_ to do
> with the standard input.

There may be something to what you say, but the behaviour experienced by the
Original Poster still needs explaining. See below.

> When you run a script, yea even a Python script, thus:
> 
>   myscript ...
> 
> then job control capable shells (all of them, these days) put the python
> process running "myscript" in its own process group as the leader
> (being, initially, the only process in the group). If myscript forks
> other processes, as happens in os.system(), they are _also_ in that
> process group. _ALL_ of them receive the SIGINT from your Ctrl-C.


I can replicate to OP's problem with these two simple Python scripts:

[steve at sylar ~]$ cat script.py
#!/usr/bin/python
print "inside script.py"
print "type Ctrl-C to exit"
while True:
    pass

[steve at sylar ~]$ cat test.py
import os
print "calling script.py with os.system"
for i in range(3):
    os.system('./script.py')


And now run them:

[steve at sylar ~]$ python test.py
calling script.py with os.system
inside script.py
type Ctrl-C to exit
Traceback (most recent call last):
  File "./script.py", line 4, in <module>
    while True:
KeyboardInterrupt
inside script.py
type Ctrl-C to exit
Traceback (most recent call last):
  File "./script.py", line 5, in <module>
    pass
KeyboardInterrupt
inside script.py
type Ctrl-C to exit
Traceback (most recent call last):
  File "./script.py", line 4, in <module>
    while True:
KeyboardInterrupt


Sure enough, I now have to hit Ctrl-C repeatedly, once per invocation of
script.py. While script.py is running, it receives the Ctrl-C, the calling
process does not.



-- 
Steven




More information about the Python-list mailing list