Difference between revisions of "Daemonise.pl"

From Organic Design wiki
(Convert script to a daemon (not finished))
 
(Updated - uses GetOpt::Long)
Line 2: Line 2:
  
 
# See daemon tutorial at http://www.webreference.com/perl/tutorial/9/
 
# See daemon tutorial at http://www.webreference.com/perl/tutorial/9/
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
+
#open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>/dev/null' or die "Can't write to /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: $!";
+
#open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
defined (my $pid = fork) or die "Can't fork: $!";
+
defined ( my $pid = fork ) or die "Can't fork: $!";
 
exit if $pid;
 
exit if $pid;
 
setsid or die "Can't start a new session: $!";
 
setsid or die "Can't start a new session: $!";
 
umask 0;
 
umask 0;
  
# START | STOP | RELOAD | STATUS
+
# Daemon related parameters
if ( grep /^[\/-]+start$/, @ARGV ) { &start; exit; }
+
if ( exists $config{ start } ) {
if ( grep /^[\/-]+stop$/, @ARGV ) { &stop; exit; }
 
if ( grep /^[\/-]+re(start|load)$/, @ARGV ) { &stop; &start; exit; }
 
if ( grep /^[\/-]+status$/, @ARGV ) { &status; exit; }
 
 
 
# Declare service functions ( START | STOP | STATUS )
 
sub start {
 
 
logAdd "Starting Peer::$peer";
 
logAdd "Starting Peer::$peer";
# chk if any in localPeers on $port/name, error and return if so
+
# chk if any in @peers on $port/name, error and return if so
`ps aux|grep `;
 
 
 
 
# Later find unique two-chr for this peer
 
# Later find unique two-chr for this peer
 
 
 
# Add the peer to inittab
 
# Add the peer to inittab
qx( 'grep -e "^[^2][^P]" /etc/inittab > /etc/inittab.tmp' );
+
#qx( 'grep -e "^[^2][^P]" /etc/inittab > /etc/inittab.tmp' );
qx( "echo \"2P:2345:respawn:$cmd\" >> /etc/inittab.tmp" );
+
#qx( "echo \"2P:2345:respawn:$cmd\" >> /etc/inittab.tmp" );
qx( 'mv /etc/inittab.tmp /etc/inittab' );
+
#qx( 'mv /etc/inittab.tmp /etc/inittab' );
 
+
exit;
 
}
 
}
  
sub stop {
+
elsif ( exists $config{ stop } ) {
 
logAdd "Stopping \"$peer\"...";
 
logAdd "Stopping \"$peer\"...";
  
Line 37: Line 30:
  
 
# 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;
 +
}
 +
 +
else ( exists $config{ restart } ) {
 +
# Signal the named peer to restart
 +
exit;
 
}
 
}
  
sub status {
+
if ( exists $config{ status } ) {
 
# - Get running peers/ports list
 
# - Get running peers/ports list
# - print info for each
+
# - also indicates how many children each parent has spawned
 +
#for (@peers) {
 +
# }
 +
exit;
 
}
 
}

Revision as of 10:17, 4 December 2005

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