summaryrefslogtreecommitdiff
path: root/manualPorts/openntpd/ntpd
blob: 7b5710e874ece3b3df62d2e390afe631d7bbe288 (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
#!/bin/bash

NAME=ntpd
USER=root
CONFIG=
RUNDIR=/var/run
PIDFILE=
STARTCMD="/usr/sbin/ntpd"
STOPCMD=
STOPTIMEOUT=120

function getpid() {
  if [ -z "$PIDFILE" ]; then
    pid="$(pgrep -xfn "$STARTCMD")"
  else
    if [ -f "$PIDFILE" ]; then
      pid=$(< $PIDFILE)
      if [ ! -d /proc/"$pid" ]; then
        echo "$NAME: removing stale pidfile $PIDFILE" >&2
        rm -f "$PIDFILE"
        unset pid
      fi
    fi
  fi
  echo "$pid"
}

case $1 in
  start)
    pid=$(getpid)
    install -d -m 755 -o $USER $RUNDIR || exit 1
    if [ -n "$pid" ]; then
      echo "$NAME already running with pid $pid" >&2
      exit 1
    fi
    eval "$STARTCMD"
    ;;
  stop)
    pid=$(getpid)
    if [ -n "$pid" ]; then
      if [ -n "$STOPCMD" ]; then
        eval "$STOPCMD"
      else
        kill "$pid"
      fi
      t=$(printf '%(%s)T' -1)
      tend=$((t+STOPTIMEOUT))
      while [ -d /proc/$pid -a $t -lt $tend ]; do
        sleep 0.5
        t=$(printf '%(%s)T' -1)
      done
      if [ -d /proc/"$pid" ]; then
        echo "$NAME still running with pid $pid" >&2
      else
        [ -n "$PIDFILE" ] && rm -f "$PIDFILE"
      fi
    else
      echo "$NAME is not running" >&2
    fi
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  status)
    pid=$(getpid)
    if [ -n "$pid" ]; then
      echo "$NAME is running with pid $pid"
    else
      echo "$NAME is not running"
    fi
    ;;
  *)
    echo "usage: $0 [start|stop|restart|status]"
    ;;
esac