#!/bin/sh # /etc/rc.d/rc.ntpd # Start, stop, and restart the Network Time Protocol daemon. # Modified to test online status, ntp.conf readability, existance # of already running process, ntpdate success, and restoring clock to local time. # Normal expectation when running ntpd from an rc.d script is that a box is # always connected (broadband). This is not the case for dialup users. Dialup # users might want to enable this script in rc.d, but should have some # feedback to understand why the script failed --- they are not yet online. # modified for colorized screen messages and local needs source /etc/functions-colors #initialize variables and locations NTPDATE=/usr/sbin/ntpdate NTPDAEMON=/usr/sbin/ntpd NTPCONFIGFILE=/etc/ntp.conf NTPLOG=/var/log/ntp/ntp.log NTPPID=/var/run/ntpd.pid # insert two appropriate regional servers here # find public servers at http://ntp.isc.org/bin/view/Servers/StratumTwoTimeServers # NTPSERVERS="ntp1.cs.wisc.edu ntp3.cs.wisc.edu" # or use the ntp system pool servers # note: always use pool servers to run ntpd (see /etc/ntp.conf), but know # that using them in this particular script will cause a second or two delay NTPSERVERS="2.us.pool.ntp.org 3.us.pool.ntp.org" Exit_Script () { if [ "$1" = "" ] ; then echo -e "${BOLDYELLOW}Exiting this script.${COLOR_RESET}" else echo -e "${BOLDYELLOW}Exiting the ${BOLDGREEN}$1${BOLDYELLOW} script.${COLOR_RESET}" fi echo exit 1 } Ping_Test () { # test online status before trying to run ntp daemon TESTURL="`echo $NTPSERVERS | awk '{print $1}'`" GOODPINGTEST="Ping test to ${BOLDGREEN}$TESTURL${COLOR_RESET} is okay." BADPINGTEST="Ping test to ${BOLDGREEN}$TESTURL${COLOR_RESET} failed." echo -e "${BOLDWHITE}Testing online status by pinging ${BOLDGREEN}$TESTURL${COLOR_RESET}." # Any test longer than two seconds probably is a DNS lookup problem. TIMEMARKER1=`date +%s` # ping once and wait one second ping -c 2 -W 2 $TESTURL &> /dev/null PINGERROR=`echo $?` TIMEMARKER2=`date +%s` if [ $(($TIMEMARKER2-$TIMEMARKER1)) -gt 2 ] ; then # let's quit right now echo "Response time was $(($TIMEMARKER2-$TIMEMARKER1)) seconds." echo "The delay time probably was caused by the DNS lookup." echo -e "You probably are not online or the IP address" echo -e " for ${BOLDGREEN}$TESTURL${COLOR_RESET} is not in the /etc/hosts file." echo -e $BADPINGTEST PINGTEST="FAILED" echo else echo -e "IP address for ${BOLDGREEN}$TESTURL${COLOR_RESET} found." #echo "IP address is either in the /etc/hosts file or you have access to a DNS server." #echo if [ "$PINGERROR" = "0" ] ; then echo -e $GOODPINGTEST PINGTEST="PASSED" else # no response, so try again but with more patience echo -e $BADPINGTEST echo echo -e "${BOLDYELLOW}No initial response--trying again.${COLOR_RESET}" ping -c 3 -W 3 $TESTURL PINGERROR=`echo $?` if [ "$PINGERROR" = "0" ] ; then echo -e $GOODPINGTEST PINGTEST="PASSED" else echo -e $BADPINGTEST PINGTEST="FAILED" echo fi fi fi export PINGTEST } # Start ntpd: ntpd_start() { # Tell the viewers what's going to happen. echo -e "${BOLDWHITE}Starting the network time protocol daemon:${COLOR_RESET}" echo " $NTPDAEMON" Ping_Test if [ "$PINGTEST" = "FAILED" ] ; then # not connected Exit_Script $0 fi # verify the config file exists #echo echo -e "${BOLDWHITE}Looking for $NTPCONFIGFILE...${COLOR_RESET}" if [ -r $NTPCONFIGFILE ]; then echo -e "The config file ${BOLDGREEN}$NTPCONFIGFILE${COLOR_RESET} exists." else echo -e "${BOLDRED}The config file ${BOLDGREEN}$NTPCONFIGFILE${BOLDRED} is missing or unreadable.${COLOR_RESET}." Exit_Script $0 fi # Is ntpd already running? #echo echo -e "${BOLDWHITE}Looking for other instances of ntpd already running...${COLOR_RESET}" NTPD_PID=`/sbin/pidof ntpd` if [ "$NTPD_PID" = "" ]; then echo "The NTP daemon does not seem to be running." else echo "NTPD_PID = $NTPD_PID" echo -e "${BOLDRED}Warning: $NTPDAEMON is already running.${COLOR_RESET}." Exit_Script $0 fi # first update the clock from a regional server to avoid gross drift errors echo -e "${BOLDWHITE}Setting clock from regional NTP servers...${COLOR_RESET}" $NTPDATE -suv $NTPSERVERS # did ntpdate succeed in connecting? If not then terminate. if [ "$?" = "0" ] ; then echo -e "${BOLDWHITE}$NTPDATE${COLOR_RESET} seems to have connected." else echo -e "${BOLDWHITE}$NTPDATE${BOLDRED} did not connect to the regional servers.${COLOR_RESET}." Exit_Script $0 fi #now ready to start ntp daemon echo -e "Starting the network time protocol daemon." $NTPDAEMON -g -c $NTPCONFIGFILE -l $NTPLOG -p $NTPPID } # Stop ntpd: ntpd_stop() { echo -e "${BOLDWHITE}Stopping the network time protocol daemon:${COLOR_RESET}" echo " $NTPDAEMON" # killall ntpd kill `cat $NTPPID` #echo sleep 1 rm -f /var/run/ntpd.pid killall ntpd 2> /dev/null } # Restart ntpd: ntpd_restart() { echo -e "${BOLDWHITE}Restarting the network time protocol daemon:${COLOR_RESET}" echo " $NTPDAEMON" ntpd_stop sleep 1 ntpd_start } case "$1" in 'start') ntpd_start ;; 'stop') ntpd_stop ;; 'restart') ntpd_restart ;; *) echo -e "${BOLDRED}Usage:${COLOR_RESET} $0 start|stop|restart" exit 1 esac