Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
| 3 | # Change this accordingly |
| 4 | ONOSDIR=${HOME}/ONOS |
| 5 | script_name="topology_rest.py" |
| 6 | |
| 7 | ####################### |
| 8 | WEBDIR=${ONOSDIR}/web |
| 9 | restscript=${WEBDIR}/$script_name |
| 10 | LOGDIR=${ONOSDIR}/onos-logs |
| 11 | REST_LOG="${LOGDIR}/rest.`hostname`.log" |
| 12 | ####################### |
| 13 | |
Ubuntu | b92ae40 | 2013-04-05 02:11:40 +0000 | [diff] [blame] | 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 | |
Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 24 | function lotate { |
| 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 |
Ubuntu | b92ae40 | 2013-04-05 02:11:40 +0000 | [diff] [blame] | 41 | dokill $p |
| 42 | # sudo kill -KILL $p |
| 43 | # echo "Killed existing prosess (pid: $p)" |
Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 44 | fi |
| 45 | done |
| 46 | } |
| 47 | |
| 48 | function status { |
| 49 | nr_process=`ps -edalf |grep ${script_name} | grep python | grep -v grep | wc -l` |
Ubuntu | b92ae40 | 2013-04-05 02:11:40 +0000 | [diff] [blame] | 50 | if [ ${nr_process} != 0 ] ; then |
Masayoshi Kobayashi | 1407a50 | 2013-02-27 06:23:08 +0000 | [diff] [blame] | 51 | echo "rest server is running" |
| 52 | else |
| 53 | echo "rest server is not running" |
Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 54 | fi |
Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | function start { |
| 58 | lotate $REST_LOG 10 |
| 59 | cd $WEBDIR |
| 60 | $restscript > $REST_LOG 2>&1 & |
| 61 | } |
| 62 | |
| 63 | case "$1" in |
| 64 | start) |
| 65 | stop |
Masayoshi Kobayashi | 7abb3a9 | 2013-03-21 22:46:04 +0000 | [diff] [blame] | 66 | sleep 2 |
Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 67 | start |
| 68 | ;; |
| 69 | stop) |
| 70 | stop |
| 71 | ;; |
Masayoshi Kobayashi | 274c07d | 2013-02-20 21:38:16 +0000 | [diff] [blame] | 72 | status) |
| 73 | status |
| 74 | ;; |
| 75 | *) |
| 76 | echo "Usage: $0 {start|stop|restart|status}" |
| 77 | exit 1 |
| 78 | esac |