Daemonise.pl

From Organic Design wiki
Revision as of 03:48, 1 December 2005 by Nad (talk | contribs) (Convert script to a daemon (not finished))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

use POSIX qw( setsid );

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

  1. START | STOP | RELOAD | STATUS

if ( grep /^[\/-]+start$/, @ARGV ) { &start; exit; } if ( grep /^[\/-]+stop$/, @ARGV ) { &stop; exit; } if ( grep /^[\/-]+re(start|load)$/, @ARGV ) { &stop; &start; exit; } if ( grep /^[\/-]+status$/, @ARGV ) { &status; exit; }

  1. Declare service functions ( START | STOP | STATUS )

sub start { logAdd "Starting Peer::$peer"; # chk if any in localPeers on $port/name, error and return if so `ps aux|grep `;

# 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' );

}

sub 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 }

sub status { # - Get running peers/ports list # - print info for each }