]> git.argeo.org Git - lgpl/argeo-commons.git/blob - sbin/osgi-service
Prepare next development cycle
[lgpl/argeo-commons.git] / sbin / osgi-service
1 #!/bin/sh
2
3 JVM=java
4 . /etc/osgiboot/osgi-service-settings.sh
5
6 APP=$1
7
8 CONF_DIR=/etc/$APP
9 if [ -f $CONF_DIR/settings.sh ];then
10 . $CONF_DIR/settings.sh
11 fi
12
13 LIB_DIR=/usr/share/$APP/lib
14
15 # read/write
16 EXEC_DIR=/var/lib/$APP
17 DATA_DIR=$EXEC_DIR/data
18 CONF_RW=$EXEC_DIR/conf
19
20 LOG_DIR=/var/log/$APP
21 LOG_FILE=$LOG_DIR/$APP.log
22
23 RUN_DIR=/var/run/$APP
24 PID_FILE=$RUN_DIR/$APP.pid
25 SHUTDOWN_FILE=$RUN_DIR/$APP.shutdown
26
27 OSGI_INSTALL_AREA=/usr/share/osgi/boot
28 OSGI_FRAMEWORK=$OSGI_INSTALL_AREA/org.eclipse.osgi.jar
29
30 RETVAL=0
31
32 start() {
33 if [ -f $PID_FILE ];then
34 PID=`cat $PID_FILE`
35 kill -0 $PID &> /dev/null
36 PID_EXISTS=$?
37 if [ $PID_EXISTS -eq 0 ]; then
38 echo $APP already running with pid $PID
39 RETVAL=1
40 return $RETVAL
41 else
42 echo Old $APP process with pid $PID is dead, removing $PID_FILE
43 rm -f $PID_FILE
44 fi
45 fi
46
47 cp --preserve $CONF_DIR/config.ini $CONF_RW/config.ini
48 touch $SHUTDOWN_FILE
49 cd $EXEC_DIR
50 $JVM \
51 -Dargeo.osgi.shutdownFile="$SHUTDOWN_FILE" \
52 -Dlog4j.configuration="file:$CONF_DIR/log4j.properties" \
53 $JAVA_OPTS -jar $OSGI_FRAMEWORK \
54 -clean \
55 -configuration "$CONF_RW" \
56 -data "$DATA_DIR" \
57 >> $LOG_FILE 2>&1 &
58 # (above) stderr redirected to stdout, then stdout to log file
59 # see http://tldp.org/LDP/abs/html/io-redirection.html
60 PID=$!
61 echo $PID > $PID_FILE
62 #echo Started $APP with pid $PID
63 return $RETVAL
64 }
65
66 stop() {
67 if [ -f $PID_FILE ];then
68 PID=`cat $PID_FILE`
69 kill -0 $PID &> /dev/null
70 PID_EXISTS=$?
71 if [ $PID_EXISTS -ne 0 ]; then
72 echo Dead $APP process with pid $PID, removing $PID_FILE
73 rm -f $PID_FILE
74 RETVAL=1
75 return $RETVAL
76 fi
77 else
78 echo $APP is not running
79 RETVAL=1
80 return $RETVAL
81 fi
82
83 # notifies application by removing the shutdown file
84 rm -f $SHUTDOWN_FILE
85
86 # wait 5 min for application to shutdown, then kill it
87 TIMEOUT=$((5*60))
88 BEGIN=$(date +%s)
89 while kill -0 $PID &> /dev/null
90 do
91 sleep 1
92 NOW=$(date +%s)
93 DURATION=$(($NOW-$BEGIN))
94 if [ $DURATION -gt $TIMEOUT ]; then
95 kill -9 $PID
96 echo Forcibly killed $APP with pid $PID
97 RETVAL=1
98 fi
99 done
100
101 # remove pid file
102 rm -f $PID_FILE
103 return $RETVAL
104
105 # timeout is only available in EL6
106 # timeout 5m sh << EOF
107 #while kill -0 $PID &> /dev/null; do sleep 1; done
108 #EOF
109 # TIMEOUT_EXIT=$?
110 # if [ $TIMEOUT_EXIT -eq 124 ];then
111 # kill -9 $PID
112 # RETVAL=1
113 # echo Killed $APP with pid $PID
114 # else
115 # echo Stopped $APP with pid $PID
116 # fi
117 # rm -f $PID_FILE
118 # return $RETVAL
119 }
120
121 status() {
122 if [ -f $PID_FILE ];then
123 PID=`cat $PID_FILE`
124 else
125 echo $APP is not running
126 return $RETVAL
127 fi
128 kill -0 $PID &> /dev/null
129 PID_EXISTS=$?
130 if [ $PID_EXISTS -eq 0 ]; then
131 echo $APP is running with pid $PID ...
132 else
133 echo No $APP process with pid $PID, removing $PID_FILE
134 rm -f $PID_FILE
135 fi
136 return $RETVAL
137 }
138
139 # main
140 case "$2" in
141 start)
142 start
143 ;;
144 stop)
145 stop
146 ;;
147 status)
148 status
149 ;;
150 *)
151 echo $"Usage: $0 {start|stop|status}"
152 exit 1
153 esac