fork and thread and signal... oooooops

Jonathan Gardner jgardn at alumni.washington.edu
Wed Mar 6 09:41:36 EST 2002


Haeyoung Kim scribbled with his keyboard:
> 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.

Good ole' SysV.

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

Okay. If you want to see if something is alive or dead, the standard way is 
to check its pid file thing. That is a file that is stored in /var/run, and 
is usually named something like atd.pid (for atd, the 'at' daemon). So, if 
yu want to call your server hyjavad (please end with a 'd'), you will use a 
pid file at hyjavad.pid. That is assuming you only want one running at any 
time.

What do you do with the pid file? When the server starts up, he should 
immediately check to see if the pid file exists. If it does, he just dies. 
There is no reason to have two of the same daemons. If it isn't there, 
create it, and put your pid in there.

When your programs dies, by signal or whatnot, you need to be sure to get 
rid of the pid file, or else you cannot start a new server. I am not sure 
if it is best to do that by watching for signals or by using _atexit. I 
think most people use signals... correct me if I am wrong.

<snip code>

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

Okay, step back, take a deep breath. Let's think about what you want to do 
in careful detail.

You have a server. You want this server to stick around no matter what. 
That is, until you want to kill it. You need it to respond to different 
signals. What it does with those signals is up to your server.

Your init.d script will be written in sh, not python. It would be a 
horrible idea to write an init.d script in python. Look at other init.d 
scripts for examples on how to do this.

Now what do you need the python script for? Do you want to make a script 
that watches the server? Why?

Jonathan






More information about the Python-list mailing list