Naoki Shiota | d5d0e94 | 2014-03-27 18:57:32 -0700 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
| 3 | # Change this accordingly |
| 4 | ONOS_HOME=${ONOS_HOME:-${HOME}/ONOS} |
| 5 | SCRIPT_NAME="topology_rest.py" |
| 6 | |
| 7 | ####################### |
| 8 | SCRIPT_DIR=${ONOS_HOME}/sample/ONS2014demo/scripts |
| 9 | REST_SCRIPT=${SCRIPT_DIR}/${SCRIPT_NAME} |
| 10 | LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs} |
| 11 | REST_LOG="${LOGDIR}/ons2014demo_rest.`hostname`.log" |
| 12 | ####################### |
| 13 | |
| 14 | dokill() { |
| 15 | for cpid in $(ps -o pid= --ppid $1) |
| 16 | do |
| 17 | dokill $cpid |
| 18 | done |
| 19 | echo "killing: $(ps -p $1 -o cmd=)" |
| 20 | kill -9 $1 > /dev/null 2>&1 |
| 21 | } |
| 22 | |
| 23 | |
| 24 | function rotate { |
| 25 | logfile=$1 |
| 26 | nr_max=${2:-10} |
| 27 | if [ -f $logfile ]; then |
| 28 | for i in `seq $(expr $nr_max - 1) -1 1`; do |
| 29 | if [ -f ${logfile}.${i} ]; then |
| 30 | mv -f ${logfile}.${i} ${logfile}.`expr $i + 1` |
| 31 | fi |
| 32 | done |
| 33 | mv $logfile $logfile.1 |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | function stop { |
| 38 | pids=`ps -edalf | grep ${SCRIPT_NAME} | grep python | grep -v grep | awk '{print $4}'` |
| 39 | for p in ${pids}; do |
| 40 | if [ x$p != "x" ]; then |
| 41 | dokill $p |
| 42 | # sudo kill -KILL $p |
| 43 | # echo "Killed existing prosess (pid: $p)" |
| 44 | fi |
| 45 | done |
| 46 | } |
| 47 | |
| 48 | function status { |
| 49 | nr_process=`ps -edalf | grep ${SCRIPT_NAME} | grep python | grep -v grep | wc -l` |
| 50 | if [ ${nr_process} != 0 ] ; then |
| 51 | echo "rest server is running" |
| 52 | else |
| 53 | echo "rest server is not running" |
| 54 | fi |
| 55 | } |
| 56 | |
| 57 | function start { |
| 58 | rotate $REST_LOG 10 |
| 59 | cd ${SCRIPT_DIR} |
| 60 | # Make log dir for iperf log files |
| 61 | if [ ! -d log ]; then |
| 62 | mkdir log |
| 63 | fi |
| 64 | $REST_SCRIPT > $REST_LOG 2>&1 & |
| 65 | } |
| 66 | |
| 67 | case "$1" in |
| 68 | start) |
| 69 | stop |
| 70 | sleep 2 |
| 71 | start |
| 72 | ;; |
| 73 | stop) |
| 74 | stop |
| 75 | ;; |
| 76 | status) |
| 77 | status |
| 78 | ;; |
| 79 | *) |
| 80 | echo "Usage: $0 {start|stop|status}" |
| 81 | exit 1 |
| 82 | esac |