SS-libev-init-Ubuntu 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: shadowsocks-libev
  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 socks5 proxy
  9. # Description: Shadowsocks-libev is a lightweight secured
  10. # socks5 proxy for embedded devices and low end boxes.
  11. ### END INIT INFO
  12. # Author: Max Lv <max.c.lv@gmail.com>
  13. # PATH should only include /usr/ if it runs after the mountnfs.sh script
  14. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  15. DESC=shadowsocks-libev # Introduce a short description here
  16. NAME=shadowsocks-libev # Introduce the short server's name here
  17. DAEMON=/usr/local/bin/ss-server # Introduce the server's location here
  18. DAEMON_ARGS="" # Arguments to run the daemon with
  19. PIDFILE=/var/run/$NAME/$NAME.pid
  20. SCRIPTNAME=/etc/init.d/shadowsocks
  21. CONFFILE=/etc/shadowsocks/config.json
  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:="nobody"}
  28. : ${GROUP:="nogroup"}
  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. # Modify the file descriptor limit
  40. ulimit -n ${MAXFD}
  41. # Take care of pidfile permissions
  42. mkdir /var/run/$NAME 2>/dev/null || true
  43. chown "$USER:$GROUP" /var/run/$NAME
  44. # Return
  45. # 0 if daemon has been started
  46. # 1 if daemon was already running
  47. # 2 if daemon could not be started
  48. start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER:$GROUP --exec $DAEMON --test > /dev/null \
  49. || return 1
  50. start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER:$GROUP --exec $DAEMON -- \
  51. -c "$CONFFILE" -u -f $PIDFILE $DAEMON_ARGS \
  52. || return 2
  53. }
  54. #
  55. # Function that stops the daemon/service
  56. #
  57. do_stop()
  58. {
  59. # Return
  60. # 0 if daemon has been stopped
  61. # 1 if daemon was already stopped
  62. # 2 if daemon could not be stopped
  63. # other if a failure occurred
  64. start-stop-daemon --stop --quiet --retry=TERM/5 --pidfile $PIDFILE --exec $DAEMON
  65. RETVAL="$?"
  66. [ "$RETVAL" = 2 ] && return 2
  67. # Wait for children to finish too if this is a daemon that forks
  68. # and if the daemon is only ever run from this initscript.
  69. # If the above conditions are not satisfied then add some other code
  70. # that waits for the process to drop all resources that could be
  71. # needed by services started subsequently. A last resort is to
  72. # sleep for some time.
  73. start-stop-daemon --stop --quiet --oknodo --retry=KILL/5 --exec $DAEMON
  74. [ "$?" = 2 ] && return 2
  75. # Many daemons don't delete their pidfiles when they exit.
  76. rm -f $PIDFILE
  77. return "$RETVAL"
  78. }
  79. case "$1" in
  80. start)
  81. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
  82. do_start
  83. case "$?" in
  84. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  85. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  86. esac
  87. ;;
  88. stop)
  89. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  90. do_stop
  91. case "$?" in
  92. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  93. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  94. esac
  95. ;;
  96. status)
  97. status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
  98. ;;
  99. restart|force-reload)
  100. log_daemon_msg "Restarting $DESC" "$NAME"
  101. do_stop
  102. case "$?" in
  103. 0|1)
  104. do_start
  105. case "$?" in
  106. 0) log_end_msg 0 ;;
  107. 1) log_end_msg 1 ;; # Old process is still running
  108. *) log_end_msg 1 ;; # Failed to start
  109. esac
  110. ;;
  111. *)
  112. # Failed to stop
  113. log_end_msg 1
  114. ;;
  115. esac
  116. ;;
  117. *)
  118. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  119. exit 3
  120. ;;
  121. esac
  122. :