local.ntpd   [plain text]


#! /usr/bin/perl -w
# 980904 Harlan Stenn - created

# vvv CHANGE THESE vvv

$ps = "/bin/ps x |";

$ntp_conf = "/etc/ntp.conf";
$ntpd = "/usr/local/bin/xntpd";
$ntpdate = "/usr/local/bin/ntpdate -b -s 10.0.0.1 10.0.0.2";

# ^^^ CHANGE THESE ^^^

{
  if (0)
    {
    }
  elsif ($ARGV[0] eq "start")
    {
      @pidlist = pidlist($ntpd);
      if (defined(@pidlist))
        {
	  warn "NTP is already running\n";
        }
      else
        {
	  if ( -f $ntp_conf && -x $ntpd )
	    {
	       system ($ntpdate);
	       system ($ntpd." -c ".$ntp_conf);
	    }
        }
    }
  elsif ($ARGV[0] eq "stop")
    {
      @pidlist = pidlist($ntpd);
      kill 'TERM', @pidlist if (scalar(@pidlist) > 0);
    }
  else
    {
      die "Usage: $0 {start,stop}\n";
    }
}

sub pidlist ($)
  {
    my ($target) = @_;
    my ($qt) = quotemeta($target);
    my @pids;

    open(PS, $ps) || die "Can't run ps: $!\n";
    while (<PS>)
      {
	chomp;
	next unless (/$qt/);
	print "Got <$_>\n";
	if (/^\s*(\d+)\s+/)
	  {
	    push @pids, $1;
	  }
      }
    close(PS);
    return @pids;
  }