Difference between revisions of "Daemonise.pl"
(Remove spawn (move to wikid.pl)) |
(Exit if not starting peer) |
||
| Line 1: | Line 1: | ||
| − | + | # Install the service into init.d and rc2-5.d | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | # Install the service | ||
| − | |||
if ( exists $config{ install } ) { | if ( exists $config{ install } ) { | ||
| − | + | # todo: check if peer of this name/port not already running | |
| − | # 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 $::cwd/$::daemon $::peer --start --dir=$::cwd --port=$::port\n"; | |
| − | my $fn = "$daemon-".lc $peer | + | symlink $target, "/etc/rc$_.d/S99$fn" for 2..5; |
| − | |||
| − | writeFile my $target = "/etc/init.d/$fn.sh", "#!/bin/sh\n$ | ||
| − | symlink $target, "/etc/rc$_.d/ | ||
} | } | ||
| Line 22: | Line 11: | ||
# - remove shell script from init.d and links from rc[2-5].d | # - remove shell script from init.d and links from rc[2-5].d | ||
if ( exists $config{ remove } ) { | if ( exists $config{ remove } ) { | ||
| − | my $fn = "$daemon-".lc $peer; | + | my $fn = "$::daemon-".lc $::peer; |
| − | unlink "/etc/rc$_.d/ | + | unlink "/etc/rc$_.d/S99$fn" for 2..5; |
unlink "/etc/init.d/$fn.sh"; | unlink "/etc/init.d/$fn.sh"; | ||
} | } | ||
| Line 29: | Line 18: | ||
# Start as a daemon | # Start as a daemon | ||
# - see http://www.webreference.com/perl/tutorial/9 for help on daemons in PERL | # - see http://www.webreference.com/perl/tutorial/9 for help on daemons in PERL | ||
| − | if ( | + | if ( exists $config{ start } ) { |
# check if peer of this name/port not already running | # check if peer of this name/port not already running | ||
| Line 63: | Line 52: | ||
} | } | ||
| − | + | else { exit } | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Revision as of 08:35, 23 February 2006
- 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 $::cwd/$::daemon $::peer --start --dir=$::cwd --port=$::port\n"; symlink $target, "/etc/rc$_.d/S99$fn" for 2..5; }
- Remove the named service and exit
- - 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"; }
- Start as a daemon
- - see http://www.webreference.com/perl/tutorial/9 for help on daemons in PERL
if ( 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; }
else { exit }



