Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi to all . I tried to make a simple and extensible skeleton ( sentinel.pl) , simple enough ( I'm new to perl), and extensible enough for me to be able to extend this script. The first task was to watch over two background scripts( greylister.pl, repeater.pl). It seems like daemon works, and death of any of background scripts is detectable for script, but one thing looks odd for me. Sentinel.pl is able to start greylister.pl\repeater.pl only if they weren't running at the moment of sentinel's first start, but if any of scripts will die later when sentinel.pl is running, this event is logged but scripts ( repeater\greylister) are not restarted.
#!/usr/bin/perl -w # Watchdog daemon, made for watching and restarting of all system elem +ents. use warnings; use POSIX; use File::Pid; use Proc::Background; use POSIX qw(setsid); use lib '/etc/squid/repeater/lib/HTTP'; my $daemonName = "sentinel"; my $dieNow = 0; my $SleepMainLoop = 5; my $logging = 1; my $logFilePath = "/var/log/"; my $logFile = $logFilePath . $daemonName . ".log"; my $pidFilePath = "/var/run/"; my $pidFile = $pidFilePath . $daemonName . ".pid"; my $greylister = Proc::Background->new('/etc/squid/all_ban.pl'); my $repeater = Proc::Background->new('/etc/squid/repeater/lib/repeater +.pl'); chdir '/'; umask 0; open STDIN, '/dev/null' or die "Can't read /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: $!"; defined( my $pid = fork ) or die "Can't fork: $!"; exit if $pid; POSIX::setsid() or die "Can't start a new session."; $SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signalHandler; $SIG{PIPE} = 'ignore'; my $pidfile = File::Pid->new( { file => $pidFile, } ); $pidfile->write or die "Can't write PID file, /dev/null: $!"; if ($logging) { open LOG, ">>$logFile"; print LOG "Sentinel started.\n"; select((select(LOG), $|=1)[0]); } until ($dieNow) { sleep($SleepMainLoop); if (!$repeater->alive()) { logEntry ("Repeater isn't running, attempting to restart") +; $repeater = Proc::Background->new('/etc/squid/repeater/lib/ +repeater.pl'); } if (!$greylister->alive()) { logEntry ("Greyliter isn't running, attempting to res +tart"); $greylister = Proc::Background->new('/etc/squid/all_b +an.pl'); } } sub logEntry { my ($logText) = @_; my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) + = localtime(time); my $dateTime = sprintf "%4d-%02d-%02d %02d:%02d:%02d", $year + 190 +0, $mon + 1, $mday, $hour, $min, $sec; if ($logging) { print LOG "$dateTime $logText\n"; } } sub signalHandler { $dieNow = 1; } END { if ($logging) { close LOG } $pidfile->remove if defined $pidfile; }

In reply to [Resolved] Perl watcher daemon by kazak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-24 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found