Difference between revisions of "Daemonise.pl"

From Organic Design wiki
(Updated - uses GetOpt::Long)
(Begin auto-install/remove functions (using rc[2-5].d))
Line 1: Line 1:
use POSIX qw( setsid );
+
# Install the service
 +
# - install a shell cmd into init.d and links to it in rc[2-5].d
 +
if ( exists $config{ install } ) {
 +
my $icmd = $cmd;
 +
my $fn = "$daemon-".lc $peer;
 +
$icmd =~ s/install/start/;
 +
writeFile $target = "/etc/init.d/$fn.sh", "#!/bin/sh\n$icmd\n";
 +
symlink $target, "/etc/rc$_.d/S09$fn" for 2..5;
 +
}
  
# See daemon tutorial at http://www.webreference.com/perl/tutorial/9/
+
# Remove the named service and exit
#open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
+
# - remove shell script from init.d and links from rc[2-5].d
#open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
+
if ( exists $config{ remove } ) {
#open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
+
my $fn = "$daemon-".lc $peer;
defined ( my $pid = fork ) or die "Can't fork: $!";
+
unlink "/etc/rc$_.d/S09$fn" for 2..5;
exit if $pid;
+
unlink "/etc/init.d/$fn.sh";
setsid or die "Can't start a new session: $!";
+
}
umask 0;
 
  
# Daemon related parameters
+
# Start as a daemon
if ( exists $config{ start } ) {
+
# - see http://www.webreference.com/perl/tutorial/9 for help on daemons in PERL
logAdd "Starting Peer::$peer";
+
if ( exists $config{ install } or exists $config{ start } ) {
# chk if any in @peers on $port/name, error and return if so
+
logAdd "Starting $daemon::$peer";
+
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
# Later find unique two-chr for this peer
+
open STDOUT, ">>$peer.log" or die "Can't write to /dev/null: $!";
+
open STDERR, ">>$peer.log" or die "Can't write to /dev/null: $!";
# Add the peer to inittab
+
defined ( my $pid = fork ) or die "Can't fork: $!";
#qx( 'grep -e "^[^2][^P]" /etc/inittab > /etc/inittab.tmp' );
+
exit if $pid;
#qx( "echo \"2P:2345:respawn:$cmd\" >> /etc/inittab.tmp" );
+
setsid or die "Can't start a new session: $!";
#qx( 'mv /etc/inittab.tmp /etc/inittab' );
+
umask 0;
exit;
 
 
}
 
}
  
 
elsif ( exists $config{ stop } ) {
 
elsif ( exists $config{ stop } ) {
logAdd "Stopping \"$peer\"...";
+
logAdd "Stopping $daemon::$peer...";
  
 
# chk if any in localPeers on $port, error if not
 
# chk if any in localPeers on $port, error if not
Line 39: Line 45:
 
}
 
}
  
else ( exists $config{ restart } ) {
+
elsif ( exists $config{ restart } ) {
 
# Signal the named peer to restart
 
# Signal the named peer to restart
 
exit;
 
exit;

Revision as of 03:01, 14 December 2005

  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 } ) { my $icmd = $cmd; my $fn = "$daemon-".lc $peer; $icmd =~ s/install/start/; writeFile $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 } ) { 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; }