3
0

run 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. start_delay=15
  8. net_down_delay=5
  9. pool="us.pool.ntp.org" # replace "us" with your country code
  10. service="${PWD##*/}"
  11. rundir="/var/run/service/$service"
  12. default_p_opt="-p 0.$pool -p 1.$pool -p 2.$pool -p 3.$pool"
  13. echo "* Checking network"
  14. test -f /var/run/service/fw/up || exec sleep $net_down_delay
  15. # With multiple interfaces (e.g. wired+wireless) going up,
  16. # networking scripts may restart ntpd service several times
  17. # in quick succession. Do not be too eager to start sending
  18. # NTP requests:
  19. sleep $start_delay
  20. # Make sure rundir/ exists
  21. mkdir -p "$rundir" 2>/dev/null
  22. chown -R "$user": "$rundir"
  23. chmod -R a=rX "$rundir"
  24. rm -rf rundir 2>/dev/null
  25. ln -s "$rundir" rundir
  26. # Grab config from dhcp
  27. cfg=-1
  28. for f in rundir/*.ntpconf; do
  29. test -f "$f" || continue
  30. . "$f"
  31. done
  32. # Select peers
  33. p_opt=""
  34. cfg=0
  35. while test x"${ntpip[$cfg]}" != x""; do
  36. p_opt="$p_opt -p ${ntpip[$cfg]}"
  37. let cfg=cfg+1
  38. done
  39. test x"$p_opt" == x"" && p_opt="$default_p_opt"
  40. if test x"$p_opt" == x""; then
  41. echo "* No NTP peers configured, stopping"
  42. svc -o .
  43. exec sleep 1
  44. fi
  45. # Let others know that we are up
  46. date '+%Y-%m-%d %H:%M:%S %Z' >rundir/up
  47. # Go go go
  48. echo "* Starting ntpd[$$]"
  49. exec \
  50. env - PATH="$PATH" \
  51. softlimit \
  52. setuidgid "$user" \
  53. ntpd -ddnNl -S ./ntp.script $p_opt