3
0

run 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # (using bashism (arrays) in dhcp config)
  3. #exec >/dev/null
  4. exec 2>&1
  5. exec </dev/null
  6. user=root
  7. pool="us.pool.ntp.org" # replace "us" with your country code
  8. service="${PWD##*/}"
  9. rundir="/var/run/service/$service"
  10. default_p_opt="-p 0.$pool -p 1.$pool -p 2.$pool -p 3.$pool"
  11. # Make sure rundir/ exists
  12. mkdir -p "$rundir" 2>/dev/null
  13. chown -R "$user:" "$rundir"
  14. chmod -R a=rX "$rundir"
  15. rm -rf rundir 2>/dev/null
  16. ln -s "$rundir" rundir
  17. echo "* Checking network"
  18. test -f /var/run/service/fw/up || exec sleep 7
  19. sleep 5 # to let it settle
  20. # Grab config from dhcp
  21. cfg=-1
  22. for f in rundir/*.ntpconf; do
  23. test -f "$f" || continue
  24. . "$f"
  25. done
  26. # Select peers
  27. p_opt=""
  28. cfg=0
  29. while test x"${ntpip[$cfg]}" != x""; do
  30. p_opt="$p_opt -p ${ntpip[$cfg]}"
  31. let cfg=cfg+1
  32. done
  33. test x"$p_opt" == x"" && p_opt="$default_p_opt"
  34. if test x"$p_opt" == x""; then
  35. echo "* No NTP peers configured, stopping"
  36. sv o .
  37. exec sleep 1
  38. fi
  39. # Let others know that we are up
  40. date '+%Y-%m-%d %H:%M:%S %Z' >rundir/up
  41. # Go go go
  42. echo "* Starting ntpd[$$]"
  43. exec \
  44. env - PATH="$PATH" \
  45. softlimit \
  46. setuidgid "$user" \
  47. ntpd -ddnNl -S ./ntp.script $p_opt