> From [EMAIL PROTECTED]  Fri Sep 25 05:04:02 1998
> Date: Fri, 25 Sep 1998 09:48:12 +0200
> From: Paul Brown <[EMAIL PROTECTED]>
> To: Linux C Programming <[EMAIL PROTECTED]>
> Subject: daemon query
> Sender: [EMAIL PROTECTED]
> Reply-To: Paul Brown <[EMAIL PROTECTED]>
> 
> Hi all,
> 
> a relatively simple question:
> 
> I am just about fnished writing a daemon, and would like to know how i
> can make the program fork into
> the background automatically..
> 
> ie, instead of this:
> 
> #> ./daemon &
> #>
> 
> i would like this:
> 
> #> ./daemon
> #>
> 
> Although it is written in perl i am sure the theory is the same...
> 
> thanks for any help you have to offer

The princples are the same. The details have the usual perlisms. The following 
is from the camel book (version 1 page 216):

FORKING A DAEMAON PROCESS

unless (fork) {         # this is the child
        unless (fork) { # this is the child's child
                sleep 1 until getppid ==1;
                LONGOPERATION;
                exit 0;
        }
        # first child exists quickly
        exit 0;
}
wait;   # parent first child quickly

The minimum code to do what you want is:

fork && exit;           # fork returns child pid only to parent
setpgrp(0, $$);         # start a new process group if you want the daemon
                        # to continue exwcuting after you logoff (nohup)

There many other security and administrative issues presented from a perl 
perspective in several sections in version 1. It is a useful companion to 
version 2 which focus more on facilities added in perl5.

I hope this is want you wanted.

Regards,



David Ross

[EMAIL PROTECTED]
Toad Technologies

"I'll be good! I will, I will !"

Reply via email to