fork and thread and signal... oooooops
Haeyoung Kim
haep1004 at dreamwiz.com
Tue Mar 5 11:46:01 EST 2002
I know little about system programming, so my code has become a
monster.
My situation is : 1) used /etc/init.d script functions such as daemon,
killproc to start/stop python script.
2) python script is composed of two tasks. launching java server
program and running script that connects that server to see if it's
alive or dead.
so, I programmed python script like this.
PID = 0
ISALIVE = 1
def exitProgram():
ISALIVE=0
killServer()
def killServer():
if PID:
os.kill(PID,SIGINT)
PID = 0
class Monitor(Thread):
def run(self):
# connect to server and check its heartbeat
# and call restartServer and exit when server is dead.
def launchServer():
pid = fork()
if not pid:
execv('java',args) #launch server
else:
PID = pid #start cheking server heartbeat
Monitor().start() #and set PID for killing server.
p, e = os.wait() #wait until server is killed
#because of killServer or restartSErver.
#hope to clean up java server when shell script 'killproc' terminate
#this script
signal.signal( SIGINT, exitProgram )
signal.signal( SIGHUP, exitProgram )
if __name__==__main__:
pid = fork()
if not pid: os.exit
else:
pid = fork()
if not pid: os.exit
else: #now start main program
while ISALIVE:
launchServer()
Yes, it's stupid. But I didn't imagined such a picture at first.
When shell script calls killproc(in init.d script), it sends signal,
and kill server-using os.kill(java_server_pid)- and get out of
os.wait() and died out generously - just my hope.
But it betrayed my hope. it throws TypeError on os.wait(), killed
anyway, but there are still my java program. :(
I think signal doesn't call exitProgram() - why?
Is there any clean solution to such a matter?
thanks in advance your kindness for reading my naive code.
More information about the Python-list
mailing list