Shadowsocks-python-init 2.1 KB

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