Finished Bill implementation. Added bill.sh to automatically send events and print...
[aquarium] / scripts / aquarium.sh
1 #!/usr/bin/env bash
2 #
3 # Copyright 2012 GRNET S.A. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 #   1. Redistributions of source code must retain the above copyright
10 #      notice, this list of conditions and the following disclaimer.
11 #
12 #  2. Redistributions in binary form must reproduce the above copyright
13 #     notice, this list of conditions and the following disclaimer in the
14 #     documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # The views and conclusions contained in the software and documentation are
29 # those of the authors and should not be interpreted as representing official
30 # policies, either expressed or implied, of GRNET S.A.
31 #
32 #
33 # Init script for Aquarium
34 #
35 #-----------------------
36 # Supported environment variables
37 #
38 # JAVA_OPTS       Runtime options for the JVM that runs Aquarium
39 #                 (default: -Xms1024M -Xmx4096M)
40 #
41 # AQUARIUM_PROP   Java system properties understood by Aquarium
42 #                 (default: -Dlog4j.debug=true)
43 #
44 # AQUARIUM_OPTS   Runtime options for Aquarium
45 #                 (default: "")
46 #
47 # AQUARIUM_HOME   Location of the top level Aquarium dir
48 #                 (default: .)
49 #----------------------
50 #
51 #set -x
52 #
53 ### BEGIN INIT INFO
54 # Provides:             aquarium
55 # Required-Start:       $all
56 # Required-Stop:        $all
57 # Should-Start:         $local_fs
58 # Should-Stop:          $local_fs
59 # Default-Start:        2 3 4 5
60 # Default-Stop:         0 1 6
61 ### END INIT INFO
62
63 # resolve links - $0 may be a softlink
64 PRG="$0"
65
66 while [ -h "$PRG" ]; do
67   ls=`ls -ld "$PRG"`
68   link=`expr "$ls" : '.*-> \(.*\)$'`
69   if expr "$link" : '/.*' > /dev/null; then
70     PRG="$link"
71   else
72     PRG=`dirname "$PRG"`/"$link"
73   fi
74 done
75
76 # Get standard environment variables
77 PRGDIR=`dirname "$PRG"`
78
79 # Only set supported environment variables if not already set
80 [ -z "$AQUARIUM_HOME" ] && AQUARIUM_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
81 [ -z "$AQUARIUM_PROP" ] && AQUARIUM_PROP=""
82 [ -z "$AQUARIUM_OPTS" ] && AQUARIUM_OPTS=""
83 [ -z "$JAVA_OPTS" ]     && JAVA_OPTS="-Xms1024M -Xmx4096M"
84
85 export AQUARIUM_HOME
86
87 PID_FILE=$AQUARIUM_HOME/bin/aquarium.pid
88
89 AQUARIUM_LIB=$AQUARIUM_HOME/lib
90 AQUARIUM_CONF=$AQUARIUM_HOME/conf
91 AQUARIUM_LOGFILE=$AQUARIUM_HOME/logs/aquarium.log
92
93 AQUARIUM_MAIN_CLASS=gr.grnet.aquarium.Main
94
95 # We use jbootstrap to start the application.
96 # No need to manually setup the CLASSPATH
97 JBOOT_JAR=$AQUARIUM_LIB/jbootstrap-3.0.0.jar
98 JBOOT_MAIN_CLASS=com.ckkloverdos.jbootstrap.Main
99
100 # Check the application status
101 check_status() {
102     if [ -f $PID_FILE ]
103     then
104         aqrunning=`ps -ef|grep java|grep $AQUARIUM_MAIN_CLASS`
105         if [ -z "$aqrunning" ]
106         then
107             return 0
108             echo "Aquarium running, but no pid file found"
109         else
110             return 1
111         fi 
112     else
113        return 0
114     fi
115     
116     return 1
117 }
118
119 # Starts the application. If "debug" is passed as argument, aquarium starts
120 # in debug mode
121 start() {
122     check_status
123     if [ $? -ne 0 ]
124     then
125         echo "Aquarium is running"
126         exit 1
127     fi
128
129     echo "Starting Aquarium"
130     echo >> $AQUARIUM_LOGFILE
131
132     CLASSPATH=$JBOOT_JAR
133
134     echo "Using CLASSPATH=$CLASSPATH"
135     echo "Using AQUARIUM_HOME=$AQUARIUM_HOME"
136     echo "Using AQUARIUM_MAIN_CLASS=$AQUARIUM_MAIN_CLASS"
137     echo "Using AQUARIUM_PROP=$AQUARIUM_PROP"
138     echo "Using JAVA_OPTS=$JAVA_OPTS"
139     echo "nohup java $JAVA_OPTS -cp $CLASSPATH $AQUARIUM_PROP $JBOOT_MAIN_CLASS -lib $AQUARIUM_LIB $AQUARIUM_MAIN_CLASS >> $AQUARIUM_LOGFILE"
140
141     nohup java $JAVA_OPTS -cp $CLASSPATH $AQUARIUM_PROP $JBOOT_MAIN_CLASS -lib $AQUARIUM_LIB $AQUARIUM_MAIN_CLASS >> $AQUARIUM_LOGFILE 2>&1 &
142     PID=$!
143     echo $PID > $PID_FILE
144     echo "PID=$PID"
145 }
146
147 # Stops the application
148 stop() {
149     check_status
150     if [ $? -eq 0 ]
151     then
152         echo "Aquarium is not running"
153         exit 1
154     fi
155
156     # Kills the application process
157     echo -n "Stopping Aquarium: "
158     kill `cat $PID_FILE`
159     rm $PID_FILE
160     echo "OK"
161 }
162
163 # Show the application status
164 status() {
165     check_status
166     if [ $? -ne 0 ]
167     then
168         echo "Aquarium is running (pid="`cat $PID_FILE`")"
169     else
170         echo "Aquarium is stopped"
171     fi
172 }
173
174 ps_aquarium() {
175   ps -ef | grep java | grep gr.grnet.aquarium.Main
176 }
177
178 forcekill() {
179   local PIDS=`ps_aquarium | awk '{print $2}'`
180   for pid in $PIDS; do
181     echo Killing $pid
182     kill -9 $pid
183   done
184 }
185
186 # Main logic, a simple case to call functions
187 case "$1" in
188   start)
189     start
190     ;;
191   stop)
192     stop
193     ;;
194   status)
195     status
196     ;;
197   restart)
198     stop
199     start
200     ;;
201   ps)
202     ps_aquarium
203     ;;
204   forcekill)
205     forcekill
206     ;;
207   *)
208       echo "Usage: $0 {start|stop|restart|status|ps|forcekill}"
209       exit 1
210     esac
211 exit 0
212
213 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
214