Writing a Perl Daemon

  • jammer2552
  • Graduate
  • Graduate
  • User avatar
  • Posts: 139

Post 3+ Months Ago

Right, I've already tried this, and I've lost a substantial amount of hair. If some kind Perl-programming soul out there could help me, I'm sure I could ask Santa to leave you some cookies this Christmas.

Here's what I want the daemon to do:
Code: [ Select ]
@hosts = ('site1.com','site2.com');
&WriteToLog('STARTED');

for(;;){
foreach $host (@hosts) {
$socket = IO::Socket::INET->new("$host:80");
if (!$socket) {
&WriteToLog("HOST DOWN: $host");
exec("curl -u USERNAME:PASSWORD -d status=\"$host is DOWN\" http://twitter.com/statuses/update.json 1>/dev/null 2>/dev/null");
}else{
close($socket);
}
}
sleep(60);
}

&WriteToLog('STOPPED');

sub WriteToLog {
my($msg) = @_;
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
$theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
open (MYFILE, '>> /etc/twitter/monitor.log');
print MYFILE "$theTime $msg\n";
close (MYFILE);
}
  1. @hosts = ('site1.com','site2.com');
  2. &WriteToLog('STARTED');
  3. for(;;){
  4. foreach $host (@hosts) {
  5. $socket = IO::Socket::INET->new("$host:80");
  6. if (!$socket) {
  7. &WriteToLog("HOST DOWN: $host");
  8. exec("curl -u USERNAME:PASSWORD -d status=\"$host is DOWN\" http://twitter.com/statuses/update.json 1>/dev/null 2>/dev/null");
  9. }else{
  10. close($socket);
  11. }
  12. }
  13. sleep(60);
  14. }
  15. &WriteToLog('STOPPED');
  16. sub WriteToLog {
  17. my($msg) = @_;
  18. @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  19. @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
  20. ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
  21. $year = 1900 + $yearOffset;
  22. $theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
  23. open (MYFILE, '>> /etc/twitter/monitor.log');
  24. print MYFILE "$theTime $msg\n";
  25. close (MYFILE);
  26. }


Much obliged to those that save my remaining sanity.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 43 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.