Daemonise.pl
use POSIX qw( setsid );
- See daemon tutorial at http://www.webreference.com/perl/tutorial/9/
- open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
- open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
- open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
defined ( my $pid = fork ) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; umask 0;
- Daemon related parameters
if ( exists $config{ start } ) { logAdd "Starting Peer::$peer"; # chk if any in @peers on $port/name, error and return if so
# Later find unique two-chr for this peer
# Add the peer to inittab #qx( 'grep -e "^[^2][^P]" /etc/inittab > /etc/inittab.tmp' ); #qx( "echo \"2P:2345:respawn:$cmd\" >> /etc/inittab.tmp" ); #qx( 'mv /etc/inittab.tmp /etc/inittab' ); exit; }
elsif ( exists $config{ stop } ) { logAdd "Stopping \"$peer\"...";
# chk if any in localPeers on $port, error if not
# Remove the peer from inittab #qx( 'grep -e "^[^2][^P]" /etc/inittab > /etc/inittab.tmp' ); #qx( 'mv /etc/inittab.tmp /etc/inittab' );
# - send a stop to peer using IO::Socket # - poll/timeout peers file to see if it dies # - kill it by pid/sig if it won't die exit; }
else ( exists $config{ restart } ) { # Signal the named peer to restart exit; }
if ( exists $config{ status } ) { # - Get running peers/ports list # - also indicates how many children each parent has spawned #for (@peers) { # } exit; }



