#!/bin/sh # Start/stop/restart ntpd. NTP_CONF=/etc/ntp.conf ntptimeset() { for i in $(gawk '/^server/ { if( $2 != "127.127.1.0" ) print $2 }' $NTP_CONF) do sntp -p no $i 2>/dev/null 1>&2 && { SYNCHRONISED=$i; break; }; done if [ "$SYNCHRONISED" ] then echo "Time synchronized with $SYNCHRONISED" else echo "Time could not be synchronized" fi } # Start ntpd: ntpd_start() { ntptimeset CMDLINE="/usr/sbin/ntpd -g" echo -n "Starting NTP daemon: $CMDLINE" $CMDLINE -p /var/run/ntpd.pid echo } # Stop ntpd: ntpd_stop() { echo -n "Stopping NTP daemon..." if [ -r /var/run/ntpd.pid ]; then kill -HUP $(cat /var/run/ntpd.pid) rm -f /var/run/ntpd.pid else killall -HUP -q ntpd fi echo } # Restart ntpd: ntpd_restart() { ntpd_stop sleep 1 ntpd_start } # Check if ntpd is running ntpd_status() { if [ -e /var/run/ntpd.pid ]; then echo "ntpd is running." else echo "ntpd is stopped." exit 1 fi } case "$1" in 'start') ntpd_start ;; 'stop') ntpd_stop ;; 'restart') ntpd_restart ;; 'status') ntpd_status ;; *) echo "usage $0 start|stop|restart|status" esac