On Tue, Aug 13, 2002 at 08:44:15PM +0200, Eliran wrote:
> Hello !
> 
> How can I start a daemon in python and later use a function to kill
it ?

The same way it's done in every other programming language?

> I used fork & kill the parent but I didn't find anyway to find the child's
> pid. Is there a better way ?

You didn't look at-all^H^H^H^H^H^H^Henough. 

fork() returns twice. It returns in the parent with the child's pid,
and it returns in the child with the value 0. 

Here's some python goodness for you:

while (1):
    # can't use os.spawn(), 1.5.2 compatibility
    pid = os.fork()
    if (pid == 0): # child
        devnullfd = os.open("/dev/null", os.O_RDONLY)
        os.dup2(devnullfd, sys.stdin.fileno())
        os.dup2(devnullfd, sys.stdout.fileno())
        os.execl("./foo");
    else: # parent
        # 'pid' contains the child's pid here
        posix.wait()
-- 
"Hmm.. Cache shrink failed - time to kill something?
 Mhwahahhaha! This is the part I really like. Giggle."
                                         -- linux/mm/vmscan.c
http://vipe.technion.ac.il/~mulix/      http://syscalltrack.sf.net

Attachment: msg20978/pgp00000.pgp
Description: PGP signature

Reply via email to