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.

132 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. # Load the VERBOSE setting and other rcS variables
  28. . /lib/init/vars.sh
  29. # Define LSB log_* functions.
  30. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  31. . /lib/lsb/init-functions
  32. #
  33. # Function that starts the daemon/service
  34. #
  35. do_start()
  36. {
  37. # Take care of pidfile permissions
  38. USER=$(egrep '\buser\s*=' $CONFFILE | sed -r 's/.*\buser\s+=\s*(.*);/\1/')
  39. mkdir /var/run/$NAME 2>/dev/null || true
  40. chown "$USER" /var/run/$NAME
  41. # Return
  42. # 0 if daemon has been started
  43. # 1 if daemon was already running
  44. # 2 if daemon could not be started
  45. start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
  46. || return 1
  47. start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
  48. -c "$CONFFILE" -f $PIDFILE \
  49. || return 2
  50. }
  51. #
  52. # Function that stops the daemon/service
  53. #
  54. do_stop()
  55. {
  56. # Return
  57. # 0 if daemon has been stopped
  58. # 1 if daemon was already stopped
  59. # 2 if daemon could not be stopped
  60. # other if a failure occurred
  61. start-stop-daemon --stop --quiet --retry=KILL/5 --pidfile $PIDFILE --name $NAME
  62. RETVAL="$?"
  63. [ "$RETVAL" = 2 ] && return 2
  64. # Wait for children to finish too if this is a daemon that forks
  65. # and if the daemon is only ever run from this initscript.
  66. # If the above conditions are not satisfied then add some other code
  67. # that waits for the process to drop all resources that could be
  68. # needed by services started subsequently. A last resort is to
  69. # sleep for some time.
  70. start-stop-daemon --stop --quiet --oknodo --retry=KILL/5 --exec $DAEMON
  71. [ "$?" = 2 ] && return 2
  72. # Many daemons don't delete their pidfiles when they exit.
  73. rm -f $PIDFILE
  74. return "$RETVAL"
  75. }
  76. case "$1" in
  77. start)
  78. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
  79. do_start
  80. case "$?" in
  81. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  82. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  83. esac
  84. ;;
  85. stop)
  86. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  87. do_stop
  88. case "$?" in
  89. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  90. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  91. esac
  92. ;;
  93. status)
  94. status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
  95. ;;
  96. restart|force-reload)
  97. log_daemon_msg "Restarting $DESC" "$NAME"
  98. do_stop
  99. case "$?" in
  100. 0|1)
  101. do_start
  102. case "$?" in
  103. 0) log_end_msg 0 ;;
  104. 1) log_end_msg 1 ;; # Old process is still running
  105. *) log_end_msg 1 ;; # Failed to start
  106. esac
  107. ;;
  108. *)
  109. # Failed to stop
  110. log_end_msg 1
  111. ;;
  112. esac
  113. ;;
  114. *)
  115. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  116. exit 3
  117. ;;
  118. esac
  119. :