exception KeyboardInterrupt and os.system command

D

darren kirby

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-----
 
D

Diez B. Roggisch

darren said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top