Date: Mon, 25 Nov 2002 18:03:40 +0000 From: Matthew Seaman <m.seaman@infracaninophile.co.uk> To: freebsd-questions@FreeBSD.ORG Subject: Re: Apache not killing subprocesses, only on FreeBSD Message-ID: <20021125180340.GB44729@happy-idiot-talk.infracaninophi> In-Reply-To: <20021125112037.W39486-100000@duey.wolves.k12.mo.us> References: <DAPNWVLH3Y2YJH5YPXHB4XZWECQKHB.3de075d8@lee> <20021125112037.W39486-100000@duey.wolves.k12.mo.us>
next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Nov 25, 2002 at 11:23:55AM -0600, Chris Dillon wrote: > On Sun, 24 Nov 2002, Lee Nelson wrote: > > > myprogram.pl reads a few parameters from STDIN, and then > > forks to work in the background: > > > > my $pid = fork; > > exit if $pid; > > die ("$pn couldn't fork $!\n") unless defined $pid; > > POSIX::setsid() > > or die ("$pn can't start a new session: $!\n"); > > > > Any clues or suggestions welcome. > > The following method to daemonize a PERL process works for me in > FreeBSD (I don't remember why I fork && exit twice, so don't ask): > > require 'sys/syscall.ph'; > > fork && exit; > syscall(&SYS_setsid) || die "Can't call setsid(): $!"; > chdir("/"); > open(STDIN, "</dev/null") || die "Can't redirect stdin: $!"; > open(STDOUT, ">/dev/null") || die "Can't redirect stdout: $!"; > open(STDERR, ">/dev/null") || die "Can't redirect stderr: $!"; > fork && exit; > The 'double fork' trick ensures that the daemon process gets re-parented as a child of init(8). As init will reap any child process that happens to die, this suppresses zombies plus it has several other effects like dissociating the daemon process from a controlling terminal. Calling setsid(2) has a similar effect, so you probably don't need to fork twice and call setsid(2). The source to the daemon(3) function in /usr/src/lib/libc/gen/daemon.c is a good example of the canonical way to do that sort of thing. Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021125180340.GB44729>