v2ray 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: v2ray
  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: V2Ray proxy services
  9. # Description: V2Ray proxy services
  10. ### END INIT INFO
  11. # Acknowledgements: Isulew Li <netcookies@gmail.com>
  12. DESC=v2ray
  13. NAME=v2ray
  14. DAEMON=/usr/bin/v2ray/v2ray
  15. PIDFILE=/var/run/$NAME.pid
  16. SCRIPTNAME=/etc/init.d/$NAME
  17. DAEMON_OPTS="-config /etc/v2ray/config.json"
  18. # Exit if the package is not installed
  19. [ -x $DAEMON ] || exit 0
  20. # Read configuration variable file if it is present
  21. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  22. # Load the VERBOSE setting and other rcS variables
  23. if [ -f "/lib/init/vars.sh"]; then
  24. . /lib/init/vars.sh
  25. fi
  26. if [ -f "/lib/lsb/init-functions"]; then
  27. # Define LSB log_* functions.
  28. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  29. . /lib/lsb/init-functions
  30. fi
  31. #
  32. # Function that starts the daemon/service
  33. #
  34. do_start()
  35. {
  36. mkdir -p /var/log/v2ray
  37. # Return
  38. # 0 if daemon has been started
  39. # 1 if daemon was already running
  40. # 2 if daemon could not be started
  41. # 3 if configuration file not ready for daemon
  42. start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
  43. || return 1
  44. start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background -m -- $DAEMON_OPTS \
  45. || return 2
  46. # Add code here, if necessary, that waits for the process to be ready
  47. # to handle requests from services started subsequently which depend
  48. # on this one. As a last resort, sleep for some time.
  49. }
  50. #
  51. # Function that stops the daemon/service
  52. #
  53. do_stop()
  54. {
  55. # Return
  56. # 0 if daemon has been stopped
  57. # 1 if daemon was already stopped
  58. # 2 if daemon could not be stopped
  59. # other if a failure occurred
  60. start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
  61. RETVAL="$?"
  62. [ "$RETVAL" = 2 ] && return 2
  63. # Wait for children to finish too if this is a daemon that forks
  64. # and if the daemon is only ever run from this initscript.
  65. # If the above conditions are not satisfied then add some other code
  66. # that waits for the process to drop all resources that could be
  67. # needed by services started subsequently. A last resort is to
  68. # sleep for some time.
  69. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
  70. [ "$?" = 2 ] && return 2
  71. # Many daemons don't delete their pidfiles when they exit.
  72. rm -f $PIDFILE
  73. return "$RETVAL"
  74. }
  75. #
  76. # Function that sends a SIGHUP to the daemon/service
  77. #
  78. do_reload() {
  79. #
  80. # If the daemon can reload its configuration without
  81. # restarting (for example, when it is sent a SIGHUP),
  82. # then implement that here.
  83. #
  84. start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE
  85. return 0
  86. }
  87. case "$1" in
  88. start)
  89. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
  90. do_start
  91. case "$?" in
  92. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  93. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  94. esac
  95. ;;
  96. stop)
  97. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  98. do_stop
  99. case "$?" in
  100. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  101. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  102. esac
  103. ;;
  104. status)
  105. status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
  106. ;;
  107. reload|force-reload)
  108. #
  109. # If do_reload() is not implemented then leave this commented out
  110. # and leave 'force-reload' as an alias for 'restart'.
  111. #
  112. log_daemon_msg "Reloading $DESC" "$NAME"
  113. do_reload
  114. log_end_msg $?
  115. ;;
  116. restart|force-reload)
  117. #
  118. # If the "reload" option is implemented then remove the
  119. # 'force-reload' alias
  120. #
  121. log_daemon_msg "Restarting $DESC" "$NAME"
  122. do_stop
  123. case "$?" in
  124. 0|1)
  125. do_start
  126. case "$?" in
  127. 0) log_end_msg 0 ;;
  128. 1) log_end_msg 1 ;; # Old process is still running
  129. *) log_end_msg 1 ;; # Failed to start
  130. esac
  131. ;;
  132. *)
  133. # Failed to stop
  134. log_end_msg 1
  135. ;;
  136. esac
  137. ;;
  138. *)
  139. #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
  140. echo "Usage: $SCRIPTNAME {start|stop|status|reload|restart|force-reload}" >&2
  141. exit 3
  142. ;;
  143. esac