2
0

srs-api 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ossrs-api(srs-api)
  4. # Required-Start: $all
  5. # Required-Stop: $all
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: ossrs-api(srs-api)
  9. # Description: https://github.com/ossrs/srs
  10. ### END INIT INFO
  11. # the config of ROOT, user must modify it when start srs from other directory,
  12. # it's ok to use the script by command ./etc/init.d/ossrs
  13. ROOT="./"
  14. APP="python ./research/api-server/server.py"
  15. CONFIG="8085"
  16. ########################################################################
  17. # utility functions
  18. ########################################################################
  19. RED="\\033[31m"
  20. GREEN="\\033[32m"
  21. YELLOW="\\033[33m"
  22. BLACK="\\033[0m"
  23. POS="\\033[60G"
  24. ok_msg(){
  25. echo -e "${1}${POS}${BLACK}[${GREEN} OK ${BLACK}]"
  26. }
  27. failed_msg(){
  28. echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]"
  29. }
  30. # load process info of srs-api
  31. # @set variable $srs_api_id to the process id.
  32. # @return 0, if process exists; otherwise:
  33. # 1, for srs-api not exists.
  34. # @set variable $error_msg if error.
  35. load_process_info() {
  36. srs_api_id=`ps aux|grep python|grep research|grep "api-server"|awk '{print $2}'`
  37. if [[ -z $srs_api_id ]]; then error_msg="srs-api process does not exists"; return 1; fi
  38. return 0;
  39. }
  40. start() {
  41. # if exists, exit.
  42. load_process_info
  43. if [[ 0 -eq $? ]]; then failed_msg "SRS-api started(pid ${srs_api_id}), should not start it again."; return 0; fi
  44. # not exists, start server
  45. ok_msg "Starting SRS-api..."
  46. # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
  47. # TODO: FIXME: write log to, for instance, the same dir of log.
  48. # TODO: FIXME: support daemon, without nohup.
  49. (cd ${ROOT}; nohup ${APP} ${CONFIG} >/dev/null 2>&1 &)
  50. # check again after start server
  51. load_process_info
  52. ret=$?; if [[ 0 -eq $? ]]; then ok_msg "SRS-api started(pid ${srs_api_id})"; return 0; fi
  53. failed_msg "SRS-api not started"
  54. return $ret
  55. }
  56. stop() {
  57. # not start, exit
  58. load_process_info
  59. if [[ 0 -ne $? ]]; then failed_msg "SRS-api not start."; return 0; fi
  60. ok_msg "Stopping SRS-api(pid ${srs_api_id})..."
  61. # process exists, kill util stop
  62. for((;;)); do
  63. load_process_info
  64. if [[ 0 -eq $? ]]; then
  65. kill -s SIGKILL ${srs_api_id} 2>/dev/null
  66. ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "send signal SIGKILL failed ret=$ret"; return $ret; fi
  67. sleep 0.1
  68. else
  69. ok_msg "SRS-api stopped"
  70. break;
  71. fi
  72. done
  73. sleep 0.1
  74. return 0
  75. }
  76. # get the status of srs-api process
  77. # @return 0 if srs-api is running; otherwise, 1 for stopped.
  78. status() {
  79. load_process_info
  80. ret=$?; if [[ 0 -eq $ret ]]; then echo "SRS-api(pid ${srs_api_id}) is running."; return 0; fi
  81. echo "SRS-api is stopped"
  82. return 1
  83. }
  84. menu() {
  85. case "$1" in
  86. start)
  87. start
  88. ;;
  89. stop)
  90. stop
  91. ;;
  92. restart)
  93. stop
  94. start
  95. ;;
  96. status)
  97. status
  98. ;;
  99. *)
  100. echo "Usage: $0 {start|stop|status|restart}"
  101. return 1
  102. ;;
  103. esac
  104. }
  105. menu $1
  106. code=$?
  107. exit ${code}