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