Minor change in script service-openmano.sh to solve bug in service start
[osm/RO.git] / scripts / service-openmano.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24
25
26 DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
27 DIRNAME=$(dirname $DIRNAME )
28 DIR_OM=$(dirname $DIRNAME )
29
30 function usage(){
31 echo -e "Usage: $0 [openmano/mano] start|stop|restart|status"
32 echo -e " Launch|Removes|Restart|Getstatus openmano on a screen"
33 }
34
35 function kill_pid(){
36 #send TERM signal and wait 5 seconds and send KILL signal ir still running
37 #PARAMS: $1: PID of process to terminate
38 kill $1 #send TERM signal
39 WAIT=5
40 while [ $WAIT -gt 0 ] && ps -o pid -U $USER -u $USER | grep -q $1
41 do
42 sleep 1
43 WAIT=$((WAIT-1))
44 [ $WAIT -eq 0 ] && echo -n "sending SIGKILL... " && kill -9 $1 #kill when count reach 0
45 done
46 echo "done"
47
48 }
49
50 #obtain parameters
51 om_list=""
52 #om_action="start" #uncoment to get a default action
53 for param in $*
54 do
55 [ "$param" == "start" -o "$param" == "stop" -o "$param" == "restart" -o "$param" == "status" ] && om_action=$param && continue
56 [ "$param" == "openmano" -o "$param" == "mano" ] && om_list="$om_list mano" && continue
57 [ "$param" == "-h" -o "$param" == "--help" ] && usage && exit 0
58
59 #if none of above, reach this line because a param is incorrect
60 echo "Unknown param '$param' type $0 --help" >&2
61 exit -1
62 done
63
64 #check action is provided
65 [ -z "$om_action" ] && usage >&2 && exit -1
66
67 #if no componenets supplied assume all
68 [ -z "$om_list" ] && om_list="mano"
69
70 for om_component in $om_list
71 do
72 [ "${om_component}" == "mano" ] && om_cmd="openmanod.py" && om_name="openmano " && om_dir=$(readlink -f ${DIR_OM})
73 #obtain PID of program
74 component_id=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
75
76 #status
77 if [ "$om_action" == "status" ]
78 then
79 [ -n "$component_id" ] && echo " $om_name running, pid $component_id"
80 [ -z "$component_id" ] && echo " $om_name stopped"
81 fi
82
83 #stop
84 if [ "$om_action" == "stop" -o "$om_action" == "restart" ]
85 then
86 #terminates program
87 [ -n "$component_id" ] && echo -n " stopping $om_name ... " && kill_pid $component_id
88 component_id=""
89 #terminates screen
90 if screen -wipe | grep -Fq .$om_component
91 then
92 screen -S $om_component -p 0 -X stuff "exit\n"
93 sleep 1
94 fi
95 fi
96
97 #start
98 if [ "$om_action" == "start" -o "$om_action" == "restart" ]
99 then
100 #calculates log file name
101 logfile=""
102 mkdir -p $DIR_OM/logs && logfile=$DIR_OM/logs/open${om_component} || echo "can not create logs directory $DIR_OM/logs"
103 #check already running
104 [ -n "$component_id" ] && echo " $om_name is already running. Skipping" && continue
105 #create screen if not created
106 echo -n " starting $om_name ... "
107 if ! screen -wipe | grep -Fq .${om_component}
108 then
109 pushd ${om_dir} > /dev/null
110 screen -dmS ${om_component} bash
111 sleep 1
112 popd > /dev/null
113 else
114 echo -n " using existing screen '${om_component}' ... "
115 screen -S ${om_component} -p 0 -X log off
116 screen -S ${om_component} -p 0 -X stuff "cd ${om_dir}\n"
117 sleep 1
118 fi
119 #move old log file index one number up and log again in index 0
120 if [[ -n $logfile ]]
121 then
122 for index in 8 7 6 5 4 3 2 1 0
123 do
124 [[ -f ${logfile}.${index} ]] && mv ${logfile}.${index} ${logfile}.$((index+1))
125 done
126 screen -S ${om_component} -p 0 -X logfile ${logfile}.0
127 screen -S ${om_component} -p 0 -X log on
128 fi
129 #launch command to screen
130 screen -S ${om_component} -p 0 -X stuff "./${om_cmd}\n"
131 #check if is running
132 [[ -n $logfile ]] && timeout=120 #2 minute
133 [[ -z $logfile ]] && timeout=20
134 while [[ $timeout -gt 0 ]]
135 do
136 #check if is running
137 #echo timeout $timeout
138 #if ! ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd}
139 log_lines=0
140 [[ -n $logfile ]] && log_lines=`head ${logfile}.0 | wc -l`
141 component_id=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
142 if [[ -z $component_id ]]
143 then #process not started or finished
144 [[ $log_lines -ge 2 ]] && echo -n "ERROR, it has exited." && break
145 #started because writted serveral lines at log so report error
146 fi
147 [[ -n $logfile ]] && grep -q "open${om_component}d ready" ${logfile}.0 && break
148 sleep 1
149 timeout=$((timeout -1))
150 done
151 if [[ -n $logfile ]] && [[ $timeout == 0 ]]
152 then
153 echo -n "timeout!"
154 else
155 echo -n "running on 'screen -x ${om_component}'."
156 fi
157 [[ -n $logfile ]] && echo " Logging at '${logfile}.0'" || echo
158 fi
159 done
160
161
162
163