Blame view

trunk/etc/init.d/srs-demo-19350 5.0 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#!/bin/bash

### BEGIN INIT INFO
# Provides:          simple-rtmp-server(srs)
# RequiRED-Start:    $all
# RequiRED-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: simple-rtmp-server(srs)
# Description:       https://github.com/winlinvip/simple-rtmp-server
### END INIT INFO

# the config of ROOT, user must modify it when start srs from other directory,
# it's ok to use the script by command ./etc/init.d/simple-rtmp-server
ROOT="./"
APP="./objs/srs"
CONFIG="./conf/demo.19350.conf"
DEFAULT_PID_FILE='./objs/srs.demo.19350.pid'
DEFAULT_LOG_FILE='./objs/srs.demo.19350.log'

########################################################################
# utility functions
########################################################################
RED="\\e[31m"
GREEN="\\e[32m"
YELLOW="\\e[33m"
BLACK="\\e[0m"
POS="\\e[60G"

ok_msg() {
    echo -e "${1}${POS}${BLACK}[${GREEN}  OK  ${BLACK}]"
}

failed_msg() {
    echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]"
}

# load process info of srs
# @set variable $srs_pid to the process id in srs.pid file.
# @return 0, if process exists; otherwise:
#       1, for pid file not exists.
#       2, for get proecess info by pid failed.
# @set variable $error_msg if error.
# @set variable $pid_file to pid file.
load_process_info() {
    # get pid file
    pid_file=`cd ${ROOT} && cat ${CONFIG} |grep ^pid|awk '{print $2}'|awk -F ';' '{print $1}'`
    if [[ -z $pid_file ]]; then pid_file=${DEFAULT_PID_FILE}; fi
    # get abs path
    pid_dir=`dirname $pid_file`
    pid_file=`(cd ${ROOT}; cd $pid_dir; pwd)`/`basename $pid_file`
    
    srs_pid=`cat $pid_file 2>/dev/null`
    ret=$?; if [[ 0 -ne $ret ]]; then error_msg="file $pid_file does not exists"; return 1; fi
    
    ps -p ${srs_pid} >/dev/null 2>/dev/null
    ret=$?; if [[ 0 -ne $ret ]]; then error_msg="process $srs_pid does not exists"; return 2; fi
    
    return 0;
}

start() {
    # if exists, exit.
    load_process_info
    if [[ 0 -eq $? ]]; then failed_msg "SRS started(pid ${srs_pid}), should not start it again."; return 0; fi
    
    # not exists, start server
    ok_msg "Starting SRS..."
    
    # get log file
    log_file=`cd ${ROOT} && cat ${CONFIG} |grep '^log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
    if [[ -z $log_file ]]; then log_file=${DEFAULT_LOG_FILE}; fi
    # get abs path
    log_dir=`dirname $log_file`
    log_file=`(cd ${ROOT} && cd $log_dir && pwd)`/`basename $log_file`
    
    # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
    if [[ -z $log_file ]]; then
        (cd ${ROOT}; ${APP} -c ${CONFIG} >/dev/null 2>&1)
    else
        (cd ${ROOT}; ${APP} -c ${CONFIG} >> $log_file 2>&1)
    fi
    
    # check again after start server
    for ((i = 0; i < 5; i++)); do
        # sleep a little while, for srs may start then crash.
        sleep 0.1
        load_process_info
        ret=$?; if [[ 0 -ne $ret ]]; then 
            failed_msg "SRS start failed"; 
            failed_msg "see $log_file"; 
            return $ret; 
        fi
    done
    
    # check whether started.
    load_process_info
    ret=$?; if [[ 0 -eq $? ]]; then ok_msg "SRS started(pid ${srs_pid})"; return 0; fi
    
    failed_msg "SRS not started"
    return $ret
}

stop() {
    # not start, exit
    load_process_info
    if [[ 0 -ne $? ]]; then failed_msg "SRS not start."; return 0; fi
    
    ok_msg "Stopping SRS(pid ${srs_pid})..."
    
    # process exists, kill util stop
    for((;;)); do
        load_process_info
        if [[ 0 -eq $? ]]; then
            kill -s SIGTERM ${srs_pid} 2>/dev/null
            ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "send signal SIGTERM failed ret=$ret"; return $ret; fi
            sleep 0.1
        else
            ok_msg "SRS stopped"
            break;
        fi
    done
    
    sleep 0.1
    return 0
}

# get the status of srs process
# @return 0 if srs is running; otherwise, 1 for stopped.
status() {
    load_process_info
    ret=$?; if [[ 0 -eq $ret ]]; then echo "SRS(pid ${srs_pid}) is running."; return 0; fi
    
    echo "SRS is stopped, $error_msg"
    return 1
}

reload() {
    # not start, exit
    load_process_info
    if [[ 0 -ne $? ]]; then failed_msg "SRS not start."; return 0; fi
    
    ok_msg "Reload SRS(pid ${srs_pid})..."
    
    # process exists, reload it
    kill -s SIGHUP ${srs_pid} 2>/dev/null
    ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "Reload SRS failed ret=$ret"; return $ret; fi
    
    load_process_info
    if [[ 0 -ne $? ]]; then failed_msg "SRS reload failed."; return $ret; fi
    
    ok_msg "SRS reloaded"
    return 0
}

menu() {
    case "$1" in
        start)
            start
            ;;
        stop)
            stop
            ;;
        restart)
            stop
            start
            ;;
        status)
            status
            ;;
        reload)
            reload
            ;;
        *)
            echo "Usage: $0 {start|stop|status|restart|reload}"
            return 1
            ;;
    esac
}

menu $1

code=$?
exit ${code}