xref: /freebsd/contrib/ntp/scripts/rc/ntpd (revision 416ba5c74546f32a993436a99516d35008e9f384)
1*2b15cb3dSCy Schubert#!/bin/sh
2*2b15cb3dSCy Schubert
3*2b15cb3dSCy SchubertNTPD=/usr/sbin/ntpd
4*2b15cb3dSCy SchubertPIDFILE=/var/run/ntpd.pid
5*2b15cb3dSCy SchubertUSER=ntp
6*2b15cb3dSCy SchubertGROUP=ntp
7*2b15cb3dSCy SchubertNTPD_OPTS="-g -u $USER:$GROUP -p $PIDFILE"
8*2b15cb3dSCy Schubert
9*2b15cb3dSCy Schubertntpd_start() {
10*2b15cb3dSCy Schubert    if [ -r $PIDFILE ]; then
11*2b15cb3dSCy Schubert        echo "ntpd seems to be already running under pid `cat $PIDFILE`."
12*2b15cb3dSCy Schubert        echo "Delete $PIDFILE if this is not the case.";
13*2b15cb3dSCy Schubert        return 1;
14*2b15cb3dSCy Schubert    fi
15*2b15cb3dSCy Schubert    echo -n "Starting NTP daemon... "
16*2b15cb3dSCy Schubert
17*2b15cb3dSCy Schubert    $NTPD $NTPD_OPTS
18*2b15cb3dSCy Schubert
19*2b15cb3dSCy Schubert    # You can't always rely on the ntpd exit code, see Bug #2420
20*2b15cb3dSCy Schubert    # case "$?" in
21*2b15cb3dSCy Schubert    #     0) echo "OK!"
22*2b15cb3dSCy Schubert    #         return 0;;
23*2b15cb3dSCy Schubert    #     *) echo "FAILED!"
24*2b15cb3dSCy Schubert    #         return 1;;
25*2b15cb3dSCy Schubert    # esac
26*2b15cb3dSCy Schubert
27*2b15cb3dSCy Schubert    sleep 1
28*2b15cb3dSCy Schubert
29*2b15cb3dSCy Schubert    if ps -Ao args|grep -q "^$NTPD $NTPD_OPTS"; then
30*2b15cb3dSCy Schubert        echo "OK!"
31*2b15cb3dSCy Schubert        return 0
32*2b15cb3dSCy Schubert    else
33*2b15cb3dSCy Schubert        echo "FAILED!"
34*2b15cb3dSCy Schubert        [ -e $PIDFILE ] && rm $PIDFILE
35*2b15cb3dSCy Schubert        return 1
36*2b15cb3dSCy Schubert    fi
37*2b15cb3dSCy Schubert}
38*2b15cb3dSCy Schubert
39*2b15cb3dSCy Schubertntpd_stop() {
40*2b15cb3dSCy Schubert    if [ ! -r $PIDFILE ]; then
41*2b15cb3dSCy Schubert        echo "ntpd doesn't seem to be running, cannot read the pid file."
42*2b15cb3dSCy Schubert        return 1;
43*2b15cb3dSCy Schubert    fi
44*2b15cb3dSCy Schubert    echo -n "Stopping NTP daemon...";
45*2b15cb3dSCy Schubert    PID=`cat $PIDFILE`
46*2b15cb3dSCy Schubert
47*2b15cb3dSCy Schubert    if kill -TERM $PID 2> /dev/null;then
48*2b15cb3dSCy Schubert        # Give ntp 15 seconds to exit
49*2b15cb3dSCy Schubert        for i in `seq 1 15`; do
50*2b15cb3dSCy Schubert            if [ -n "`ps -p $PID|grep -v PID`" ]; then
51*2b15cb3dSCy Schubert                echo -n .
52*2b15cb3dSCy Schubert                sleep 1
53*2b15cb3dSCy Schubert            else
54*2b15cb3dSCy Schubert                echo " OK!"
55*2b15cb3dSCy Schubert                rm $PIDFILE
56*2b15cb3dSCy Schubert                return 0
57*2b15cb3dSCy Schubert            fi
58*2b15cb3dSCy Schubert        done
59*2b15cb3dSCy Schubert    fi
60*2b15cb3dSCy Schubert
61*2b15cb3dSCy Schubert    echo " FAILED! ntpd is still running";
62*2b15cb3dSCy Schubert    return 1
63*2b15cb3dSCy Schubert}
64*2b15cb3dSCy Schubert
65*2b15cb3dSCy Schubertntpd_status() {
66*2b15cb3dSCy Schubert    if [ -r $PIDFILE ]; then
67*2b15cb3dSCy Schubert        echo "NTP daemon is running as `cat $PIDFILE`"
68*2b15cb3dSCy Schubert    else
69*2b15cb3dSCy Schubert        echo "NTP daemon is not running"
70*2b15cb3dSCy Schubert    fi
71*2b15cb3dSCy Schubert}
72*2b15cb3dSCy Schubert
73*2b15cb3dSCy Schubertcase "$1" in
74*2b15cb3dSCy Schubert    'start')
75*2b15cb3dSCy Schubert        ntpd_start
76*2b15cb3dSCy Schubert        ;;
77*2b15cb3dSCy Schubert    'stop')
78*2b15cb3dSCy Schubert        ntpd_stop
79*2b15cb3dSCy Schubert        ;;
80*2b15cb3dSCy Schubert    'restart')
81*2b15cb3dSCy Schubert        ntpd_stop && ntpd_start
82*2b15cb3dSCy Schubert        ;;
83*2b15cb3dSCy Schubert    'status')
84*2b15cb3dSCy Schubert        ntpd_status
85*2b15cb3dSCy Schubert        ;;
86*2b15cb3dSCy Schubert    *)
87*2b15cb3dSCy Schubert        echo "Usage: $0 (start|stop|restart|status)"
88*2b15cb3dSCy Schubertesac
89