Daemonise.pl

From Organic Design wiki
Revision as of 10:17, 4 December 2005 by Nad (talk | contribs) (Updated - uses GetOpt::Long)

use POSIX qw( setsid );

  1. See daemon tutorial at http://www.webreference.com/perl/tutorial/9/
  2. open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
  3. open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
  4. 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;

  1. 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; }