1
0

SS-python-init 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. ### BEGIN INIT INFO
  5. # Provides: SS
  6. # Required-Start: $local_fs $remote_fs $network $syslog
  7. # Required-Start: $local_fs $remote_fs $network $syslog
  8. # Default-Start: 2 3 4 5
  9. # Default-Stop: 0 1 6
  10. # Short-Description: SS - SS-python starts daemon
  11. # Description: SS - lightweight secured scoks5 proxy
  12. ### END INIT INFO
  13. #
  14. # chkconfig: - 90 10
  15. # description: SS start/stop/status/restart script
  16. SS_bin=/usr/local/python/bin/ssserver
  17. SS_conf=/etc/shadowsocks/config.json
  18. #SS_USAGE is the message if this script is called without any options
  19. SS_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
  20. #SHUTDOWN_WAIT is wait time in seconds for SS proccess to stop
  21. SHUTDOWN_WAIT=20
  22. SS_pid(){
  23. echo `ps -ef | grep $SS_bin | grep -v grep | tr -s " "|cut -d" " -f2`
  24. }
  25. start() {
  26. pid=$(SS_pid)
  27. if [ -n "$pid" ]; then
  28. echo -e "\e[00;31mSS is already running (pid: $pid)\e[00m"
  29. else
  30. $SS_bin -c $SS_conf -d start
  31. RETVAL=$?
  32. if [ "$RETVAL" = "0" ]; then
  33. echo -e "\e[00;32mStarting SS\e[00m"
  34. else
  35. echo -e "\e[00;32mSS start Failed\e[00m"
  36. fi
  37. status
  38. fi
  39. return 0
  40. }
  41. status(){
  42. pid=$(SS_pid)
  43. if [ -n "$pid" ]; then
  44. echo -e "\e[00;32mSS is running with pid: $pid\e[00m"
  45. else
  46. echo -e "\e[00;31mSS is not running\e[00m"
  47. fi
  48. }
  49. stop(){
  50. pid=$(SS_pid)
  51. if [ -n "$pid" ]; then
  52. echo -e "\e[00;31mStoping SS\e[00m"
  53. $SS_bin -c $SS_conf -d stop
  54. let kwait=$SHUTDOWN_WAIT
  55. count=0;
  56. until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
  57. do
  58. echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n";
  59. sleep 1
  60. let count=$count+1;
  61. done
  62. if [ $count -gt $kwait ]; then
  63. echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
  64. kill -9 $pid
  65. fi
  66. else
  67. echo -e "\e[00;31mSS is not running\e[00m"
  68. fi
  69. return 0
  70. }
  71. case $1 in
  72. start)
  73. start
  74. ;;
  75. stop)
  76. stop
  77. ;;
  78. restart)
  79. stop
  80. start
  81. ;;
  82. status)
  83. status
  84. ;;
  85. *)
  86. echo -e $SS_USAGE
  87. ;;
  88. esac
  89. exit 0