]> git.argeo.org Git - lgpl/argeo-commons.git/blob - osgi-service
03897d637401a2f78f9ada9a992430724d9399af
[lgpl/argeo-commons.git] / 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 -Dorg.osgi.framework.security=osgi \
54 -Djava.security.policy="file:/etc/osgiboot/all.policy" \
55 $JAVA_OPTS -jar $OSGI_FRAMEWORK \
56 -clean \
57 -configuration "$CONF_RW" \
58 -data "$DATA_DIR" \
59 >> $LOG_FILE 2>&1 &
60 # (above) stderr redirected to stdout, then stdout to log file
61 # see http://tldp.org/LDP/abs/html/io-redirection.html
62 PID=$!
63 echo $PID > $PID_FILE
64 #echo Started $APP with pid $PID
65 return $RETVAL
66 }
67
68 stop() {
69 if [ -f $PID_FILE ];then
70 PID=`cat $PID_FILE`
71 kill -0 $PID &> /dev/null
72 PID_EXISTS=$?
73 if [ $PID_EXISTS -ne 0 ]; then
74 echo Dead $APP process with pid $PID, removing $PID_FILE
75 rm -f $PID_FILE
76 RETVAL=1
77 return $RETVAL
78 fi
79 else
80 echo $APP is not running
81 RETVAL=1
82 return $RETVAL
83 fi
84
85 # notifies application by removing the shutdown file
86 rm -f $SHUTDOWN_FILE
87
88 # wait 5 min for application to shutdown, then kill it
89 TIMEOUT=$((5*60))
90 BEGIN=$(date +%s)
91 while kill -0 $PID &> /dev/null
92 do
93 sleep 1
94 NOW=$(date +%s)
95 DURATION=$(($NOW-$BEGIN))
96 if [ $DURATION -gt $TIMEOUT ]; then
97 kill -9 $PID
98 echo Forcibly killed $APP with pid $PID
99 RETVAL=1
100 fi
101 done
102
103 # remove pid file
104 rm -f $PID_FILE
105 return $RETVAL
106
107 # timeout is only available in EL6
108 # timeout 5m sh << EOF
109 #while kill -0 $PID &> /dev/null; do sleep 1; done
110 #EOF
111 # TIMEOUT_EXIT=$?
112 # if [ $TIMEOUT_EXIT -eq 124 ];then
113 # kill -9 $PID
114 # RETVAL=1
115 # echo Killed $APP with pid $PID
116 # else
117 # echo Stopped $APP with pid $PID
118 # fi
119 # rm -f $PID_FILE
120 # return $RETVAL
121 }
122
123 status() {
124 if [ -f $PID_FILE ];then
125 PID=`cat $PID_FILE`
126 else
127 echo $APP is not running
128 return $RETVAL
129 fi
130 kill -0 $PID &> /dev/null
131 PID_EXISTS=$?
132 if [ $PID_EXISTS -eq 0 ]; then
133 echo $APP is running with pid $PID ...
134 else
135 echo No $APP process with pid $PID, removing $PID_FILE
136 rm -f $PID_FILE
137 fi
138 return $RETVAL
139 }
140
141 # main
142 case "$2" in
143 start)
144 start
145 ;;
146 stop)
147 stop
148 ;;
149 status)
150 status
151 ;;
152 *)
153 echo $"Usage: $0 {start|stop|status}"
154 exit 1
155 esac