2
0

srs-demo-19350 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ossrs(srs)
  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(srs)
  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="./objs/srs"
  15. CONFIG="./conf/demo.19350.conf"
  16. DEFAULT_PID_FILE='./objs/srs.demo.19350.pid'
  17. DEFAULT_LOG_FILE='./objs/srs.demo.19350.log'
  18. ########################################################################
  19. # utility functions
  20. ########################################################################
  21. RED="\\033[31m"
  22. GREEN="\\033[32m"
  23. YELLOW="\\033[33m"
  24. BLACK="\\033[0m"
  25. POS="\\033[60G"
  26. ok_msg() {
  27. echo -e "${1}${POS}${BLACK}[${GREEN} OK ${BLACK}]"
  28. }
  29. failed_msg() {
  30. echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]"
  31. }
  32. # load process info of srs
  33. # @set variable $srs_pid to the process id in srs.pid file.
  34. # @return 0, if process exists; otherwise:
  35. # 1, for pid file not exists.
  36. # 2, for get proecess info by pid failed.
  37. # @set variable $error_msg if error.
  38. # @set variable $pid_file to pid file.
  39. load_process_info() {
  40. # get pid file
  41. pid_file=`cd ${ROOT} && cat ${CONFIG} |grep ^pid|awk '{print $2}'|awk -F ';' '{print $1}'`
  42. if [[ -z $pid_file ]]; then pid_file=${DEFAULT_PID_FILE}; fi
  43. # get abs path
  44. pid_dir=`dirname $pid_file`
  45. pid_file=`(cd ${ROOT}; cd $pid_dir; pwd)`/`basename $pid_file`
  46. srs_pid=`cat $pid_file 2>/dev/null`
  47. ret=$?; if [[ 0 -ne $ret ]]; then error_msg="file $pid_file does not exists"; return 1; fi
  48. ps -p ${srs_pid} >/dev/null 2>/dev/null
  49. ret=$?; if [[ 0 -ne $ret ]]; then error_msg="process $srs_pid does not exists"; return 2; fi
  50. return 0;
  51. }
  52. start() {
  53. # if exists, exit.
  54. load_process_info
  55. if [[ 0 -eq $? ]]; then failed_msg "SRS started(pid ${srs_pid}), should not start it again."; return 0; fi
  56. # not exists, start server
  57. ok_msg "Starting SRS..."
  58. # get log file
  59. log_file=`cd ${ROOT} && cat ${CONFIG} |grep '^log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
  60. if [[ -z $log_file ]]; then log_file=${DEFAULT_LOG_FILE}; fi
  61. # get abs path
  62. log_dir=`dirname $log_file`
  63. log_file=`(cd ${ROOT} && cd $log_dir && pwd)`/`basename $log_file`
  64. # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
  65. if [[ -z $log_file ]]; then
  66. (cd ${ROOT}; ${APP} -c ${CONFIG} >/dev/null 2>&1)
  67. else
  68. (cd ${ROOT}; ${APP} -c ${CONFIG} >> $log_file.sys 2>&1)
  69. fi
  70. # check again after start server
  71. for ((i = 0; i < 5; i++)); do
  72. # sleep a little while, for srs may start then crash.
  73. sleep 0.1
  74. load_process_info
  75. ret=$?; if [[ 0 -ne $ret ]]; then
  76. failed_msg "SRS start failed";
  77. failed_msg "see $log_file";
  78. return $ret;
  79. fi
  80. done
  81. # check whether started.
  82. load_process_info
  83. ret=$?; if [[ 0 -eq $? ]]; then ok_msg "SRS started(pid ${srs_pid})"; return 0; fi
  84. failed_msg "SRS not started"
  85. return $ret
  86. }
  87. stop() {
  88. # not start, exit
  89. load_process_info
  90. if [[ 0 -ne $? ]]; then failed_msg "SRS not start."; return 0; fi
  91. ok_msg "Stopping SRS(pid ${srs_pid})..."
  92. # process exists, kill util stop
  93. for((;;)); do
  94. load_process_info
  95. if [[ 0 -eq $? ]]; then
  96. kill -s SIGTERM ${srs_pid} 2>/dev/null
  97. ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "send signal SIGTERM failed ret=$ret"; return $ret; fi
  98. sleep 0.1
  99. else
  100. ok_msg "SRS stopped"
  101. break;
  102. fi
  103. done
  104. sleep 0.1
  105. return 0
  106. }
  107. # get the status of srs process
  108. # @return 0 if srs is running; otherwise, 1 for stopped.
  109. status() {
  110. load_process_info
  111. ret=$?; if [[ 0 -eq $ret ]]; then echo "SRS(pid ${srs_pid}) is running."; return 0; fi
  112. echo "SRS is stopped, $error_msg"
  113. return 1
  114. }
  115. reload() {
  116. # not start, exit
  117. load_process_info
  118. if [[ 0 -ne $? ]]; then failed_msg "SRS not start."; return 0; fi
  119. ok_msg "Reload SRS(pid ${srs_pid})..."
  120. # process exists, reload it
  121. kill -s SIGHUP ${srs_pid} 2>/dev/null
  122. ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "Reload SRS failed ret=$ret"; return $ret; fi
  123. load_process_info
  124. if [[ 0 -ne $? ]]; then failed_msg "SRS reload failed."; return $ret; fi
  125. ok_msg "SRS reloaded"
  126. return 0
  127. }
  128. menu() {
  129. case "$1" in
  130. start)
  131. start
  132. ;;
  133. stop)
  134. stop
  135. ;;
  136. restart)
  137. stop
  138. start
  139. ;;
  140. status)
  141. status
  142. ;;
  143. reload)
  144. reload
  145. ;;
  146. *)
  147. echo "Usage: $0 {start|stop|status|restart|reload}"
  148. return 1
  149. ;;
  150. esac
  151. }
  152. menu $1
  153. code=$?
  154. exit ${code}