Home | History | Annotate | Line # | Download | only in scripts
nanny.pl revision 1.1.1.2
      1 #!/usr/bin/perl
      2 #
      3 # Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      4 #
      5 # This Source Code Form is subject to the terms of the Mozilla Public
      6 # License, v. 2.0. If a copy of the MPL was not distributed with this
      7 # file, you can obtain one at https://mozilla.org/MPL/2.0/.
      8 #
      9 # See the COPYRIGHT file distributed with this work for additional
     10 # information regarding copyright ownership.
     11 
     12 # A simple nanny to make sure named stays running.
     13 
     14 $pid_file_location = '/var/run/named.pid';
     15 $nameserver_location = 'localhost';
     16 $dig_program = 'dig';
     17 $named_program =  'named';
     18 
     19 fork() && exit();
     20 
     21 for (;;) {
     22 	$pid = 0;
     23 	open(FILE, $pid_file_location) || goto restart;
     24 	$pid = <FILE>;
     25 	close(FILE);
     26 	chomp($pid);
     27 
     28 	$res = kill 0, $pid;
     29 
     30 	goto restart if ($res == 0);
     31 
     32 	$dig_command =
     33 	       "$dig_program +short . \@$nameserver_location > /dev/null";
     34 	$return = system($dig_command);
     35 	goto restart if ($return == 9);
     36 
     37 	sleep 30;
     38 	next;
     39 
     40  restart:
     41 	if ($pid != 0) {
     42 		kill 15, $pid;
     43 		sleep 30;
     44 	}
     45 	system ($named_program);
     46 	sleep 120;
     47 }
     48