Ditch debugger attachment. There will be other ways to debug
[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 # resolve links - $0 may be a softlink
54 PRG="$0"
55
56 while [ -h "$PRG" ]; do
57   ls=`ls -ld "$PRG"`
58   link=`expr "$ls" : '.*-> \(.*\)$'`
59   if expr "$link" : '/.*' > /dev/null; then
60     PRG="$link"
61   else
62     PRG=`dirname "$PRG"`/"$link"
63   fi
64 done
65
66 # Get standard environment variables
67 PRGDIR=`dirname "$PRG"`
68
69 # Only set supported environment variables if not already set
70 [ -z "$AQUARIUM_HOME" ] && AQUARIUM_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
71 [ -z "$AQUARIUM_PROP" ] && AQUARIUM_PROP="-Dlog4j.debug=true"
72 [ -z "$AQUARIUM_OPTS" ] && AQUARIUM_OPTS=""
73 [ -z "$JAVA_OPTS" ]     && JAVA_OPTS="-Xms1024M -Xmx4096M"
74
75
76 AQMAIN=gr.grnet.aquarium.Main
77 PID=$AQUARIUM_HOME/bin/aquarium.pid
78 LIB=$AQUARIUM_HOME/lib
79 LOG=$AQUARIUM_HOME/logs/aquarium.log
80 CONF=$AQUARIUM_HOME/conf
81
82 # Check the application status
83 check_status() {
84     if [ -f $PID ]
85     then
86         aqrunning=`ps -ef|grep java|grep aquarium`
87         if [ -z "$aqrunning" ]
88         then
89             return 0
90             echo "Aquarium running, but no pid file found"
91         else
92             return 1
93         fi 
94     else
95        return 0
96     fi
97     
98     return 1
99 }
100
101 # Starts the application. If "debug" is passed as argument, aquarium starts
102 # in debug mode
103 start() {
104     check_status
105     if [ $? -ne 0 ]
106     then
107         echo "Aquarium is running"
108         exit 1
109     fi
110
111     echo "Starting Aquarium"
112
113     # Build classpath
114     CLASSPATH=`find $LIB -type f|grep jar$|tr '\n' ':'|sed -e 's/\:$//'`
115     
116     # load log4j from classpath
117     CLASSPATH=$CONF:$CLASSPATH
118
119     echo "Using CLASSPATH $CLASSPATH"
120     echo "Using AQUARIUM_HOME $AQUARIUM_HOME"
121     echo "Using MAIN $AQMAIN"
122     echo "Using AQUARIUM_PROP $AQUARIUM_PROP"
123     echo "Using JAVA_OPTS $JAVA_OPTS"
124
125     java $JAVA_OPTS -cp $CLASSPATH $AQUARIUM_PROP $AQMAIN >> $LOG 2>&1 &
126     echo $! > $PID
127     echo "OK [pid = $!]"
128 }
129
130 # Stops the application
131 stop() {
132     check_status
133     if [ $? -eq 0 ]
134     then
135         echo "Aquarium is not running"
136         exit 1
137     fi
138
139     # Kills the application process
140     echo -n "Stopping Aquarium: "
141     kill `cat $PID`
142     rm $PID
143     echo "OK"
144 }
145
146 # Show the application status
147 status() {
148     check_status
149     if [ $? -ne 0 ]
150     then
151         echo "Aquarium is running (pid=$pid)"
152     else
153         echo "Aquarium is stopped"
154     fi
155 }
156
157 # Main logic, a simple case to call functions
158 case "$1" in
159     start)
160         start
161         ;;
162     stop)
163         stop
164         ;;
165     status)
166         status
167         ;;
168     debug)
169         start "debug"
170         ;;
171     restart)
172         stop
173         start
174         ;;
175     *)
176         echo "Usage: $0 {start|stop|restart|status|debug}"
177         exit 1
178 esac
179
180 exit 0
181
182 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
183