smartdns 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2018-2024 Ruilin Peng (Nick) <[email protected]>.
  4. #
  5. # smartdns is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # smartdns is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ### BEGIN INIT INFO
  18. # Provides: smartdns
  19. # Required-Start: $network
  20. # Required-Stop: $network
  21. # Default-Start: 2 3 4 5
  22. # Default-Stop:
  23. # Short-Description: Start smartdns server
  24. ### END INIT INFO
  25. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  26. . /etc/default/smartdns
  27. SMARTDNS=/usr/sbin/smartdns
  28. PIDFILE=/run/smartdns.pid
  29. if [ ! -d "/run" ]; then
  30. PIDFILE=/var/run/smartdns.pid
  31. fi
  32. test -x $SMARTDNS || exit 5
  33. case $1 in
  34. start)
  35. $SMARTDNS "$SMART_DNS_OPTS" -R
  36. while true; do
  37. if [ -e "$PIDFILE" ]; then
  38. break;
  39. fi
  40. sleep .5
  41. done
  42. PID="$(cat $PIDFILE 2>/dev/null)"
  43. if [ -z "$PID" ]; then
  44. echo "start smartdns server failed."
  45. exit 1
  46. fi
  47. if [ ! -e "/proc/$PID" ]; then
  48. echo "start smartdns server failed."
  49. exit 1
  50. fi
  51. echo "start smartdns server success."
  52. ;;
  53. stop)
  54. if [ ! -f "$PIDFILE" ]; then
  55. echo "smartdns server is stopped."
  56. exit 0
  57. fi
  58. PID="$(cat $PIDFILE 2>/dev/null)"
  59. if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
  60. echo "smartdns server is stopped"
  61. exit 0
  62. fi
  63. kill -TERM "$PID"
  64. if [ $? -ne 0 ]; then
  65. echo "Stop smartdns server failed."
  66. exit 1;
  67. fi
  68. LOOP=1
  69. while true; do
  70. if [ ! -d "/proc/$PID" ]; then
  71. break;
  72. fi
  73. if [ $LOOP -gt 12 ]; then
  74. kill -9 "$PID"
  75. break;
  76. fi
  77. LOOP=$((LOOP+1))
  78. sleep .5
  79. done
  80. echo "Stop smartdns server success."
  81. ;;
  82. restart)
  83. "$0" stop && "$0" start
  84. ;;
  85. status)
  86. PID="$(cat "$PIDFILE" 2>/dev/null)"
  87. if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
  88. echo "smartdns server is not running."
  89. exit 1
  90. fi
  91. echo "smartdns server is running."
  92. status=0
  93. ;;
  94. *)
  95. echo "Usage: $0 {start|stop|restart|status}"
  96. exit 2
  97. ;;
  98. esac
  99. exit $status