Difference between revisions of "Daemonise.pl"

From Organic Design wiki
(Generalise child spawning for snipit execution)
(Fix bugs in spawn())
Line 73: Line 73:
 
# Function for spawning a child to execute a function
 
# Function for spawning a child to execute a function
 
sub spawn {
 
sub spawn {
$subname = shift;
+
my $subname = shift;
$subref = eval '\&\$subname';
+
my $subref = eval '\&$'.'subname';
 
$SIG{CHLD} = 'IGNORE';
 
$SIG{CHLD} = 'IGNORE';
if ( defined( my $pid = fork ) ) { $pid ? logAdd "Spawned child ($pid) for \"$subname\"" : exit &$subref $::subname = $subname }
+
if ( defined( my $pid = fork ) ) {
 +
if ( $pid ) { logAdd "Spawned child ($pid) for \"$subname\"" }
 +
else { $::subname = $subname; exit &$subref; }
 +
}
 
else { logAdd "Cannot fork a child for \"$subname\": $!" }
 
else { logAdd "Cannot fork a child for \"$subname\": $!" }
 
}
 
}

Revision as of 23:02, 4 February 2006

  1. Get list of running peers
  2. for ( `ps x` ) { push @peers, [$2,$1,$3] if /^\s*(\d+).+?peerd \((.*):(.*)\)/ }
  1. Get status of all running peers

if ( exists $config{ remove } ) { }

  1. Install the service
  2. - install a shell cmd into init.d and links to it in rc[2-5].d

if ( exists $config{ install } ) {

# check if peer of this name/port not already running

my $icmd = $cmd; my $fn = "$daemon-".lc $peer; $icmd =~ s/install/start/; writeFile my $target = "/etc/init.d/$fn.sh", "#!/bin/sh\n$icmd\n"; symlink $target, "/etc/rc$_.d/S09$fn" for 2..5; }

  1. Remove the named service and exit
  2. - remove shell script from init.d and links from rc[2-5].d

if ( exists $config{ remove } ) { my $fn = "$daemon-".lc $peer; unlink "/etc/rc$_.d/S09$fn" for 2..5; unlink "/etc/init.d/$fn.sh"; }

  1. Start as a daemon
  2. - see http://www.webreference.com/perl/tutorial/9 for help on daemons in PERL

if ( exists $config{ install } or exists $config{ start } ) {

# check if peer of this name/port not already running

logAdd "Starting $daemon::$peer"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, ">>$peer.log" or die "Can't write to /dev/null: $!"; open STDERR, ">>$peer.log" 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; }

elsif ( exists $config{ stop } ) { logAdd "Stopping $daemon::$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; }

elsif ( 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; }

  1. Function for spawning a child to execute a function

sub spawn { my $subname = shift; my $subref = eval '\&$'.'subname'; $SIG{CHLD} = 'IGNORE'; if ( defined( my $pid = fork ) ) { if ( $pid ) { logAdd "Spawned child ($pid) for \"$subname\"" } else { $::subname = $subname; exit &$subref; } } else { logAdd "Cannot fork a child for \"$subname\": $!" } }