ntp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: ntp
  4. # Required-Start: $network networking
  5. # Required-Stop:
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop:
  8. # Short-Description: Start NTP daemon
  9. ### END INIT INFO
  10. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  11. . /lib/lsb/init-functions
  12. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  13. DESC="network time protocol daemon"
  14. NTPDATE_BIN=/usr/sbin/ntpdate
  15. DAEMON=/usr/sbin/ntpd
  16. PIDFILE=/var/run/ntpd.pid
  17. test -x $DAEMON || exit 5
  18. if [ -r /etc/default/ntp ]; then
  19. . /etc/default/ntp
  20. fi
  21. if [ -e /var/lib/ntp/ntp.conf.dhcp ]; then
  22. NTPD_OPTS="$NTPD_OPTS -c /var/lib/ntp/ntp.conf.dhcp"
  23. fi
  24. LOCKFILE=/var/lock/ntpdate
  25. lock_ntpdate() {
  26. if [ -x /usr/bin/lockfile-create ]; then
  27. lockfile-create $LOCKFILE
  28. lockfile-touch $LOCKFILE &
  29. LOCKTOUCHPID="$!"
  30. fi
  31. }
  32. unlock_ntpdate() {
  33. if [ -x /usr/bin/lockfile-create ] ; then
  34. kill $LOCKTOUCHPID
  35. lockfile-remove $LOCKFILE
  36. fi
  37. }
  38. RUNASUSER=ntp
  39. UGID=$(getent passwd $RUNASUSER | cut -f 3,4 -d:) || true
  40. if test "$(uname -s)" = "Linux"; then
  41. NTPD_OPTS="$NTPD_OPTS -u $UGID"
  42. fi
  43. case $1 in
  44. start)
  45. #add
  46. cat /etc/asterisk/ntp.conf | tail -n 4 | tr -d " " > /tmp/ntp.conf.val
  47. . /tmp/ntp.conf.val
  48. sed -i "/export TZ/d" /etc/profile
  49. echo 'server 127.127.1.0 # local clock' >/etc/ntp.conf
  50. echo 'fudge 127.127.1.0 stratum 2' >>/etc/ntp.conf
  51. if [ $TIMEZONE ] ; then
  52. echo $TIMEZONE > /etc/TZ
  53. echo "export TZ=`cat /etc/TZ`" >> /etc/profile
  54. export TZ=$TIMEZONE
  55. ln -sf /usr/share/zoneinfo/$TZNAME /etc/localtime
  56. fi
  57. if [ $NTP = "yes" ] ; then
  58. if [ -x $NTPDATE_BIN ] ; then
  59. echo -n "Getting initial time via ntp"
  60. $NTPDATE_BIN $NTPDATE_OPTS $NTPserver > /dev/null 2>&1
  61. echo "."
  62. fi
  63. fi
  64. #end add for user
  65. log_daemon_msg "Starting NTP server" "ntpd"
  66. if [ -z "$UGID" ]; then
  67. log_failure_msg "user \"$RUNASUSER\" does not exist"
  68. exit 1
  69. fi
  70. lock_ntpdate
  71. start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -- -p $PIDFILE $NTPD_OPTS
  72. status=$?
  73. unlock_ntpdate
  74. log_end_msg $status
  75. ;;
  76. stop)
  77. log_daemon_msg "Stopping NTP server" "ntpd"
  78. start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
  79. log_end_msg $?
  80. rm -f $PIDFILE
  81. ;;
  82. restart|force-reload)
  83. $0 stop && sleep 2 && $0 start
  84. ;;
  85. try-restart)
  86. if $0 status >/dev/null; then
  87. $0 restart
  88. else
  89. exit 0
  90. fi
  91. ;;
  92. reload)
  93. exit 3
  94. ;;
  95. status)
  96. status_of_proc $DAEMON "NTP server"
  97. ;;
  98. *)
  99. echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
  100. exit 2
  101. ;;
  102. esac