Problem: using signal.alarm() to stop a run-away os.system() command

M

mkent

I'm trying to use signal.alarm to stop a run-away os.system command.
Can anyone exlain the following behavior?

Given following the trivial program:

import os
import signal

def timeoutHandler(signum, frame):
print "Timeout"
raise ValueError


signal.signal(signal.SIGALRM, timeoutHandler)
signal.alarm(5)

os.system("yes")

signal.alarm(0)


What I expect is that the Linux/UNIX 'yes' command (which runs until
terminated) would be stopped after 5 seconds, when the timeoutHandler
is called, thus raising a ValueError and terminating this example
program. Instead, the 'yes' command run until it is terminated (by,
say, a kill command), at which time the timeoutHandler is called. In
other words, the running of the 'yes' command acts like it is blocking
the SIGALRM signal until it terminates, at which time the SIGALRM
signal is raised. This is quite surprising, and totally defeats my
ability to catch a run-away process. Can anyone see what I'm doing
wrong?
 

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

No members online now.

Forum statistics

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

Latest Threads

Top