On Tue, 2002-08-13 at 10:17, Ahmed Moustafa wrote:
> Drieux wrote:
> >
> > On Monday, August 12, 2002, at 11:31 , Ahmed Moustafa wrote:
> >
> >> Drieux wrote:
> >>
> >>> what you want is
> >>> how do I create a demon process
> >>> that will be able to run independent of my programme:
> >>> cf:
> >>> http://www.wetware.com/drieux/pbl/Sys/daemon1.txt
> >>
> >>
> >> If it's not possible to use Proc::Daemon, what would be the other
> >> options,
> >> please?
> >
> >
> > do what you will need to do to daemonize -
> > reset STDIN/STDOUT/STDERR to other FD - in
> > the above case I reset them to a unique log,
> > then it is time to do a 'double fork' of the
> > process into the background - making sure to
> > set the session id enroute.... That normally
> > makes sure that one is no longer attached to
> > a controlling TTY...
>
> Could you give a sample code, please?
>
> --Ahmed Moustafa
Proc::Daemon is a pure Perl module, so it is a perfect Perl example of
how to create a daemon in Unix. Is there a particular reason you cannot
use Proc::Daemon (uncooperative sysadmins)? If push comes to shove you
could do the unthinkable and just manually copy the code over instead of
using the module.
<snip href="Proc::Daemon">
##---------------------------------------------------------------------------##
## File:
## @(#) Daemon.pm 1.2 99/04/17 01:02:34
## Author:
## Earl Hood [EMAIL PROTECTED]
##---------------------------------------------------------------------------##
## Copyright (C) 1997-1999 Earl Hood, [EMAIL PROTECTED]
## All rights reserved.
##
## This program is free software; you can redistribute it and/or
## modify it under the same terms as Perl itself.
##---------------------------------------------------------------------------##
package Proc::Daemon;
use strict;
use vars qw( $VERSION @ISA @EXPORT_OK );
use Exporter;
@ISA = qw( Exporter );
$VERSION = "0.02";
@EXPORT_OK = qw( Fork OpenMax );
##---------------------------------------------------------------------------##
use Carp;
use POSIX;
##---------------------------------------------------------------------------##
## Fork(): Try to fork if at all possible. Function will croak
## if unable to fork.
##
sub Fork {
my($pid);
FORK: {
if (defined($pid = fork)) {
return $pid;
} elsif ($! =~ /No more process/) {
sleep 5;
redo FORK;
} else {
croak "Can't fork: $!";
}
}
}
##---------------------------------------------------------------------------##
## OpenMax(): Return the maximum number of possible file descriptors.
## If sysconf() does not give us value, we punt with our own value.
##
sub OpenMax {
my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
(!defined($openmax) || $openmax < 0) ? 64 : $openmax;
}
##---------------------------------------------------------------------------##
## Init(): Become a daemon.
##
sub Init {
my $oldmode = shift || 0;
my($pid, $sess_id, $i);
## Fork and exit parent
if ($pid = Fork) { exit 0; }
## Detach ourselves from the terminal
croak "Cannot detach from controlling terminal"
unless $sess_id = POSIX::setsid();
## Prevent possibility of acquiring a controling terminal
if (!$oldmode) {
$SIG{'HUP'} = 'IGNORE';
if ($pid = Fork) { exit 0; }
}
## Change working directory
chdir "/";
## Clear file creation mask
umask 0;
## Close open file descriptors
foreach $i (0 .. OpenMax) { POSIX::close($i); }
## Reopen stderr, stdout, stdin to /dev/null
open(STDIN, "+>/dev/null");
open(STDOUT, "+>&STDIN");
open(STDERR, "+>&STDIN");
$oldmode ? $sess_id : 0;
}
*init = \&Init;
##---------------------------------------------------------------------------##
1;
</snip>
--
Today is Setting Orange the 6th day of Bureaucracy in the YOLD 3168
Kallisti!
Missile Address: 33:48:3.521N 84:23:34.786W
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]