[Tutor] os.system sending of break signals

Johan Geldenhuys johan at accesstel.co.za
Fri Oct 28 09:00:39 CEST 2005

So far:
I tried
 >>> f = os.popen('ping -c 100 > cap2.txt')

You will see that I send the output from the command to a file, because 
I want to test how stop the command before it reaches 100 pings.
If I don't write the output to the file 'cap2.txt' and succeeds in 
closing 'f', all the data returned to 'f' will be lost.

If i use:
Then 'f' is not closed and the commands until 100 packets has been send.

So, I am still looking for a way to stop the command before it 
terminates by itself. I will not be able to get the process ID in all he 
cases to kill it that way.

Then I tried :
 >>> f = os.popen2('ping -c 100 > cap3.txt')
 >>> f
(<open file '<fdopen>', mode 'w' at 0x40389530>, <open file '<fdopen>', 
mode 'r' at 0x404a69f8>)
 >>> f[0].close()
 >>> f[1].close()
 >>> f
(<closed file '<fdopen>', mode 'w' at 0x40389530>, <closed file 
'<fdopen>', mode 'r' at 0x404a69f8>)

This as you can see closes the fd's but the command is not terminated, 
because I can see my filesize is growing.

Any other Ideas or what am I doing wrong?


Alan Gauld wrote:

>> I send a command to os.system(cmd) and want to send a break signal in 
>> the same way. Is this possible? The break signal is ctrl c (^c).
>> I tried this, but it didn't work: os.system('\x03') I think Hex 03 is 
>> the signal for ctrl c.
> Its not possible with os.system because os.system runs the called 
> program to completion in a separate process. You would need to mess 
> about with threads and process ids to do it that way. However if you 
> use popen() (or maybe popen2()? )you should be able to send a Ctrl-C 
> character to the process. However whether that actually terminates it 
> will depend on the OS, the process and whether Ctrl-C is being caught 
> anywhere. But at least there's a chance of it working!
> HTH,
> Alan G
> Author of the learn to program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld

More information about the Tutor mailing list