summaryrefslogtreecommitdiff
path: root/scripts/rc/ntpd
blob: 9896247e77a0a4b320141ea2ccbd4215cbc12a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh

NTPD=/usr/sbin/ntpd
PIDFILE=/var/run/ntpd.pid
USER=ntp
GROUP=ntp
NTPD_OPTS="-g -u $USER:$GROUP -p $PIDFILE"

ntpd_start() {
    if [ -r $PIDFILE ]; then
        echo "ntpd seems to be already running under pid `cat $PIDFILE`."
        echo "Delete $PIDFILE if this is not the case.";
        return 1;
    fi
    echo -n "Starting NTP daemon... "

    $NTPD $NTPD_OPTS

    # You can't always rely on the ntpd exit code, see Bug #2420
    # case "$?" in
    #     0) echo "OK!"
    #         return 0;;
    #     *) echo "FAILED!"
    #         return 1;;
    # esac

    sleep 1

    if ps -Ao args|grep -q "^$NTPD $NTPD_OPTS"; then
        echo "OK!"
        return 0
    else
        echo "FAILED!"
        [ -e $PIDFILE ] && rm $PIDFILE
        return 1
    fi
}

ntpd_stop() {
    if [ ! -r $PIDFILE ]; then
        echo "ntpd doesn't seem to be running, cannot read the pid file."
        return 1;
    fi
    echo -n "Stopping NTP daemon...";
    PID=`cat $PIDFILE`

    if kill -TERM $PID 2> /dev/null;then
        # Give ntp 15 seconds to exit
        for i in `seq 1 15`; do
            if [ -n "`ps -p $PID|grep -v PID`" ]; then
                echo -n .
                sleep 1
            else
                echo " OK!"
                rm $PIDFILE
                return 0
            fi
        done
    fi

    echo " FAILED! ntpd is still running";
    return 1
}

ntpd_status() {
    if [ -r $PIDFILE ]; then
        echo "NTP daemon is running as `cat $PIDFILE`"
    else
        echo "NTP daemon is not running"
    fi
}

case "$1" in
    'start')
        ntpd_start
        ;;
    'stop')
        ntpd_stop
        ;;
    'restart')
        ntpd_stop && ntpd_start
        ;;
    'status')
        ntpd_status
        ;;
    *)
        echo "Usage: $0 (start|stop|restart|status)"
esac