how to detach process

Dilton McGowan II diltonm at pacbell.net
Sun Aug 25 02:26:29 EDT 2002


"Noah" <noah at noah.org> wrote in message
news:c9d82136.0208232259.4191800 at posting.google.com...
> alienoid <alienoid at is.lg.ua> wrote in message
news:<mailman.1030137902.27254.python-list at python.org>...
> > Hello python-list users,
> >
> > I need your help with this case:
> > Program from comp A calls program written in python on comp B. The
> > program on comp B calls program C and should quit and not beeing
> > blocked waiting when program C finish(program C will be a long running
> > task). What mechanism in python should I use in program B to implement
> > that?
> >
> > Thanks in advance
>
> I'm not sure what comp A has to do with it.
> It seems that your problem is the same even if described without comp A.
>
> It sounds like you want to create a daemon process using Python.
> In UNIX this requires a "double fork" to detatch a process
> from the controlling terminal (login shell). The following code
> gives the basic daemon outline. In your case you would probably want the
> main() function to exec program C. See the os module for 'exec' functions.
> There are many of them:
>     execl, execle, execlp, execlpe, execv, execve, execvp, execvpe
>
> ### DAEMON
###################################################################
> import os, sys, time
>
> def main ():
>     '''This is the main function run by the daemon.
>     This just writes the process id to a file then
>     sits in a loop and writes numbers to the file once a second.
>     You could also put an exec in here to switch to a different program.
>     '''
>     fout = open ('daemon', 'a')
>     fout.write ('daemon started with pid %d\n' % os.getpid())
>     c = 0
>     while 1:
>         fout.write ('Count: %d\n' % c)
>         fout.flush()
>         c = c + 1
>         time.sleep(1)
>
> #
> # This is interesting section that does the daemon magic.
> #
> pid = os.fork ()
> if pid == 0: # if pid is child
>     os.setsid() # Start new process group.
>     pid = os.fork () Second fork will start detatched process.
>     if pid == 0: # if pid is child
>         main ()
>
> ### END
######################################################################
>
> Yours,
> Noah Spurrier

Could this technique be used by someone as a CGI parasitical spawn, sucking
CPU cycles long after the original CGI died?





More information about the Python-list mailing list