You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.6 KiB

  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: shadowsocks
  4. # Required-Start: $network $local_fs $remote_fs
  5. # Required-Stop: $remote_fs
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: lightweight secured scoks5 proxy
  9. # Description: Shadowsocks-libev is a lightweight secured
  10. # scoks5 proxy for embedded devices and low end boxes.
  11. #
  12. ### END INIT INFO
  13. # Author: Max Lv <max.c.lv@gmail.com>
  14. # PATH should only include /usr/ if it runs after the mountnfs.sh script
  15. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  16. DESC=shadowsocks # Introduce a short description here
  17. NAME=shadowsocks # Introduce the short server's name here
  18. DAEMON=/usr/bin/ss-server # Introduce the server's location here
  19. DAEMON_ARGS="" # Arguments to run the daemon with
  20. PIDFILE=/var/run/$NAME/$NAME.pid
  21. SCRIPTNAME=/etc/init.d/$NAME
  22. # Exit if the package is not installed
  23. [ -x $DAEMON ] || exit 0
  24. # Read configuration variable file if it is present
  25. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  26. [ "$START" = "yes" ] || exit 0
  27. : ${USER:="root"}
  28. : ${GROUP:="root"}
  29. # Load the VERBOSE setting and other rcS variables
  30. . /lib/init/vars.sh
  31. # Define LSB log_* functions.
  32. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  33. . /lib/lsb/init-functions
  34. #
  35. # Function that starts the daemon/service
  36. #
  37. do_start()
  38. {
  39. # Take care of pidfile permissions
  40. mkdir /var/run/$NAME 2>/dev/null || true
  41. chown "$USER:$GROUP" /var/run/$NAME
  42. # Return
  43. # 0 if daemon has been started
  44. # 1 if daemon was already running
  45. # 2 if daemon could not be started
  46. start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER:$GROUP --exec $DAEMON --test > /dev/null \
  47. || return 1
  48. start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER:$GROUP --exec $DAEMON -- \
  49. -c "$CONFFILE" -f $PIDFILE \
  50. || return 2
  51. }
  52. #
  53. # Function that stops the daemon/service
  54. #
  55. do_stop()
  56. {
  57. # Return
  58. # 0 if daemon has been stopped
  59. # 1 if daemon was already stopped
  60. # 2 if daemon could not be stopped
  61. # other if a failure occurred
  62. start-stop-daemon --stop --quiet --retry=KILL/5 --pidfile $PIDFILE --name $NAME
  63. RETVAL="$?"
  64. [ "$RETVAL" = 2 ] && return 2
  65. # Wait for children to finish too if this is a daemon that forks
  66. # and if the daemon is only ever run from this initscript.
  67. # If the above conditions are not satisfied then add some other code
  68. # that waits for the process to drop all resources that could be
  69. # needed by services started subsequently. A last resort is to
  70. # sleep for some time.
  71. start-stop-daemon --stop --quiet --oknodo --retry=KILL/5 --exec $DAEMON
  72. [ "$?" = 2 ] && return 2
  73. # Many daemons don't delete their pidfiles when they exit.
  74. rm -f $PIDFILE
  75. return "$RETVAL"
  76. }
  77. case "$1" in
  78. start)
  79. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
  80. do_start
  81. case "$?" in
  82. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  83. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  84. esac
  85. ;;
  86. stop)
  87. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  88. do_stop
  89. case "$?" in
  90. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  91. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  92. esac
  93. ;;
  94. status)
  95. status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
  96. ;;
  97. restart|force-reload)
  98. log_daemon_msg "Restarting $DESC" "$NAME"
  99. do_stop
  100. case "$?" in
  101. 0|1)
  102. do_start
  103. case "$?" in
  104. 0) log_end_msg 0 ;;
  105. 1) log_end_msg 1 ;; # Old process is still running
  106. *) log_end_msg 1 ;; # Failed to start
  107. esac
  108. ;;
  109. *)
  110. # Failed to stop
  111. log_end_msg 1
  112. ;;
  113. esac
  114. ;;
  115. *)
  116. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  117. exit 3
  118. ;;
  119. esac
  120. :