Difference between revisions of "Daemonise.pl"

From Organic Design wiki
(Convert script to a daemon (not finished))
 
 
(21 intermediate revisions by 2 users not shown)
Line 1: Line 1:
use POSIX qw( setsid );
+
<perl>
 +
#!/usr/bin/perl
 +
# Install/remove should use update-rc.d
 +
# - see http://newbiedoc.berlios.de/wiki/Runlevels_introduction
 +
# - Actually best to leave as is because redhat uses chkconfig not update-rc.d
 +
#  but the current peer way is working for both
  
# See daemon tutorial at http://www.webreference.com/perl/tutorial/9/
+
# Install the service into init.d and rc2-5.d
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
+
if ( exists $config{ install } ) {
open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
+
# todo: check if peer of this name/port not already running
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: $!";
+
writeFile my $target = "/etc/init.d/$fn.sh",
exit if $pid;
+
"#!/bin/sh\n/usr/bin/perl $::dir/$::daemon $::peer --start --dir=$::dir/\n";
setsid or die "Can't start a new session: $!";
+
symlink $target, "/etc/rc$_.d/S99$fn" for 2..5;
umask 0;
+
chmod 0755, "/etc/init.d/$fn.sh";
 +
logAdd "$fn.sh added to /etc/init.d";
 +
}
  
# START | STOP | RELOAD | STATUS
+
# Remove the named service and exit
if ( grep /^[\/-]+start$/, @ARGV ) { &start; exit; }
+
# - remove shell script from init.d and links from rc[2-5].d
if ( grep /^[\/-]+stop$/, @ARGV ) { &stop; exit; }
+
if ( exists $config{ remove } ) {
if ( grep /^[\/-]+re(start|load)$/, @ARGV ) { &stop; &start; exit; }
+
my $fn = "$::daemon-" . lc $::peer;
if ( grep /^[\/-]+status$/, @ARGV ) { &status; exit; }
+
unlink "/etc/rc$_.d/S99$fn" for 2..5;
 +
unlink "/etc/init.d/$fn.sh";
 +
logAdd "$fn.sh removed from /etc/init.d";
 +
}
  
# Declare service functions ( START | STOP | STATUS )
+
# Start as a daemon
sub start {
+
# - see http://www.webreference.com/perl/tutorial/9 for help on daemons in PERL
logAdd "Starting Peer::$peer";
+
if ( exists $config{ start } ) {
# chk if any in localPeers on $port/name, error and return if so
+
# todo: check if peer of this name/port not already running
`ps aux|grep `;
+
logAdd "Starting $::daemon::$::peer";
+
open STDIN, '/dev/null';
# Later find unique two-chr for this peer
+
open STDOUT, ">>../$::peer.log";
+
open STDERR, ">>../$::peer.log";
# 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;
 +
}
  
}
+
elsif ( exists $config{ stop } ) {
 
+
logAdd "Stopping $::daemon::$::peer...";
sub stop {
 
logAdd "Stopping \"$peer\"...";
 
  
 
# chk if any in localPeers on $port, error if not
 
# chk if any in localPeers on $port, error if not
  
 
# Remove the peer from inittab
 
# Remove the peer from inittab
qx( 'grep -e "^[^2][^P]" /etc/inittab > /etc/inittab.tmp' );
+
#qx( 'grep -e "^[^2][^P]" /etc/inittab > /etc/inittab.tmp' );
qx( 'mv /etc/inittab.tmp /etc/inittab' );
+
#qx( 'mv /etc/inittab.tmp /etc/inittab' );
 
 
  
 
# - send a stop to peer using IO::Socket
 
# - send a stop to peer using IO::Socket
 
# - poll/timeout peers file to see if it dies
 
# - poll/timeout peers file to see if it dies
 
# - kill it by pid/sig if it won't die
 
# - kill it by pid/sig if it won't die
}
+
exit;
 +
}
 +
 
 +
elsif ( exists $config{ restart } ) {
 +
# Signal the named peer to restart
 +
exit;
 +
}
  
sub status {
+
else { exit }
# - Get running peers/ports list
+
</perl>
# - print info for each
+
[[Category:PERL]]
}
 

Latest revision as of 13:14, 8 December 2011

<perl>

  1. !/usr/bin/perl
  2. Install/remove should use update-rc.d
  3. - see http://newbiedoc.berlios.de/wiki/Runlevels_introduction
  4. - Actually best to leave as is because redhat uses chkconfig not update-rc.d
  5. but the current peer way is working for both
  1. Install the service into init.d and rc2-5.d

if ( exists $config{ install } ) { # todo: check if peer of this name/port not already running my $fn = "$::daemon-" . lc $::peer; writeFile my $target = "/etc/init.d/$fn.sh", "#!/bin/sh\n/usr/bin/perl $::dir/$::daemon $::peer --start --dir=$::dir/\n"; symlink $target, "/etc/rc$_.d/S99$fn" for 2..5; chmod 0755, "/etc/init.d/$fn.sh"; logAdd "$fn.sh added to /etc/init.d"; }

  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/S99$fn" for 2..5; unlink "/etc/init.d/$fn.sh"; logAdd "$fn.sh removed from /etc/init.d"; }

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

if ( exists $config{ start } ) { # todo: check if peer of this name/port not already running logAdd "Starting $::daemon::$::peer"; open STDIN, '/dev/null'; open STDOUT, ">>../$::peer.log"; open STDERR, ">>../$::peer.log"; 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; }

else { exit } </perl>