v2ray 4.2 KB

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