From owner-freebsd-questions Mon Nov 25 10: 4: 1 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4CCCA37B401 for ; Mon, 25 Nov 2002 10:03:59 -0800 (PST) Received: from smtp.infracaninophile.co.uk (happy-idiot-talk.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4251D43EAA for ; Mon, 25 Nov 2002 10:03:57 -0800 (PST) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk (localhost.infracaninophile.co.uk [IPv6:::1]) by smtp.infracaninophile.co.uk (8.12.6/8.12.6) with ESMTP id gAPI3jQf045048 for ; Mon, 25 Nov 2002 18:03:45 GMT (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost) by happy-idiot-talk.infracaninophile.co.uk (8.12.6/8.12.6/Submit) id gAPI3ewu045047 for freebsd-questions@FreeBSD.ORG; Mon, 25 Nov 2002 18:03:40 GMT Date: Mon, 25 Nov 2002 18:03:40 +0000 From: Matthew Seaman To: freebsd-questions@FreeBSD.ORG Subject: Re: Apache not killing subprocesses, only on FreeBSD Message-ID: <20021125180340.GB44729@happy-idiot-talk.infracaninophi> Mail-Followup-To: Matthew Seaman , freebsd-questions@FreeBSD.ORG References: <20021125112037.W39486-100000@duey.wolves.k12.mo.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20021125112037.W39486-100000@duey.wolves.k12.mo.us> User-Agent: Mutt/1.5.1i X-Spam-Status: No, hits=-3.0 required=5.0 tests=IN_REP_TO,QUOTED_EMAIL_TEXT,REFERENCES,SPAM_PHRASE_00_01, USER_AGENT,USER_AGENT_MUTT version=2.43 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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, " 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