Difference between revisions of "Daemonise.pl"
({{perl}}{{#security:*|dev}}{{#security:view|*}} update) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | <perl> | |
+ | #!/usr/bin/perl | ||
# Install/remove should use update-rc.d | # Install/remove should use update-rc.d | ||
# - see http://newbiedoc.berlios.de/wiki/Runlevels_introduction | # - see http://newbiedoc.berlios.de/wiki/Runlevels_introduction | ||
Line 8: | Line 9: | ||
if ( exists $config{ install } ) { | if ( exists $config{ install } ) { | ||
# todo: check if peer of this name/port not already running | # todo: check if peer of this name/port not already running | ||
− | my $fn = "$::daemon-".lc $::peer; | + | my $fn = "$::daemon-" . lc $::peer; |
writeFile my $target = "/etc/init.d/$fn.sh", | writeFile my $target = "/etc/init.d/$fn.sh", | ||
"#!/bin/sh\n/usr/bin/perl $::dir/$::daemon $::peer --start --dir=$::dir/\n"; | "#!/bin/sh\n/usr/bin/perl $::dir/$::daemon $::peer --start --dir=$::dir/\n"; | ||
Line 14: | Line 15: | ||
chmod 0755, "/etc/init.d/$fn.sh"; | chmod 0755, "/etc/init.d/$fn.sh"; | ||
logAdd "$fn.sh added to /etc/init.d"; | logAdd "$fn.sh added to /etc/init.d"; | ||
− | + | } | |
# Remove the named service and exit | # Remove the named service and exit | ||
# - 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/S99$fn" for 2..5; | unlink "/etc/rc$_.d/S99$fn" for 2..5; | ||
unlink "/etc/init.d/$fn.sh"; | unlink "/etc/init.d/$fn.sh"; | ||
logAdd "$fn.sh removed from /etc/init.d"; | logAdd "$fn.sh removed from /etc/init.d"; | ||
− | + | } | |
# Start as a daemon | # Start as a daemon | ||
Line 37: | Line 38: | ||
setsid or die "Can't start a new session: $!"; | setsid or die "Can't start a new session: $!"; | ||
umask 0; | umask 0; | ||
− | + | } | |
elsif ( exists $config{ stop } ) { | elsif ( exists $config{ stop } ) { | ||
Line 52: | Line 53: | ||
# - kill it by pid/sig if it won't die | # - kill it by pid/sig if it won't die | ||
exit; | exit; | ||
− | + | } | |
elsif ( exists $config{ restart } ) { | elsif ( exists $config{ restart } ) { | ||
# Signal the named peer to restart | # Signal the named peer to restart | ||
exit; | exit; | ||
− | + | } | |
else { exit } | else { exit } | ||
+ | </perl> | ||
+ | [[Category:PERL]] |
Latest revision as of 13:14, 8 December 2011
<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
- 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"; }
- 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"; logAdd "$fn.sh removed from /etc/init.d"; }
- Start as a daemon
- - 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>