Want to monitor process..

Jonathan Gardner jgardn at alumni.washington.edu
Thu Feb 28 02:43:53 EST 2002


Haeyoung Kim wrote:

> I want to run or kill a process(program?) using python.

from os import *
# Start a process
...
pid = fork()
if not pid:
        # I'm a child!
        exec*(what you want to do)
# I'm a parent
...

# Kill a process:
kill(pid)

Check out the documentation on 'os' and especially section 6.1.5 in the lib 
documentation. Forget about anything that says "Windows only" or "for 
Windows". Just focus on anything that says "Unix only".

> Once it's started, I don't want to run the same process any more. (but
> can be killed or restarted, of course)

Then keep track of which files you have started. Maybe a dict with the keys 
being the files you would like to start and the values being the pid?

> At present I use a naive way to write a pid file when initiate the
> process, but in that case I can't restart it when the process has been
> down. (i.e., if that when the process has gone, I should kill the
> processes and remove pid file manually.)

Check out the module "atexit". You can set it up so that when the process 
is dying, it will call the functions. One of the functions should remove 
the pid lock file.

What you are talking about now is daemons that run in the background. Let's 
say you don't want to accidentally start two webservers. First off, if you 
tried to start two webservers, only one of them could listen to the port of 
choice. The other guy would have problems.

The pidfile is useful when you want to send a signal to THE webserver. You 
do something like:
$ kill -SIG_HUP `cat /the/pidfile`
with Bash. (man kill)

You can also test if the daemon is still running by seeing if the pidfile 
is there.

> I'm not so good at such a system programming.. :(

No one really is. You'll have to find an approach and a system that works 
for your needs. There are so many choices on Unix it is crazy. After doing 
sytems programming on Unix, though, you will feel like you are wearing a 
straightjacket and blinders when you are programming on Windows. That is, 
if you are still sane afterwards.

If you are going to do a lot of system programming, you should become 
familiar with C and the way things are done in Unix. (comp.unix.programmers 
is a good group to post this question to.)

> Is there any nice solution?

Look at how Apache handles its children and itself. You don't have to dive 
into the source, just understand the basics. I think Apache is probably one 
of the best out there. You may also want to try to understand how X windows 
works, how sendmail works, and how telnetd, inetd, sshd and stuff work to 
get a better picture of systems programming. You'll find that at least ONE 
of the programs on your system does something very similar to what you want 
to do. Trust the programs that have been around since the beginning of 
time, and don't try something revolutionary unless you really have to.

Jonathan




More information about the Python-list mailing list