Project

General

Profile

« Previous | Next » 

Revision 25876144

Added by Andreas Kohlbecker almost 11 years ago

first commits towards fixing #2324

View differences:

src/main/installer/linux/cdmserver
1
#! /bin/sh
1
#! /bin/sh -e
2
#
3
# /etc/init.d/cdmserver -- startup script for cdmserver
4
#
5
# Author: Andreas Kohlbecker <a.kohlbecker@bgbm.org>
6
#
2 7
### BEGIN INIT INFO
3 8
# Provides:          CDM Server
4 9
# Required-Start:    $local_fs $remote_fs
5 10
# Required-Stop:     $local_fs $remote_fs
6 11
# Default-Start:     2 3 4 5
7 12
# Default-Stop:      0 1 6
8
# Short-Description: Starts CDMServer daemon
9
# Description:       Starts CDMServer daemon which provides a RESTful webservice to access CDM Stores
13
# Short-Description: Start CDM Server daemon
14
# Description:       Starts CDM Server daemon which provides a webservice to access CDM Stores
10 15
### END INIT INFO
11 16

  
17
# Configuration files are found in
18
# /etc/cdmserver/.cdmLibrary
19

  
12 20
# install init script:		update-rc.d cdmserver defaults
13 21
# uninstall init script:	update-rc.d -f cdmserver remove
14 22

  
15
# Author: Andreas Kohlbecker
16

  
17
# Do NOT "set -e"
18

  
19 23
# PATH should only include /usr/* if it runs after the mountnfs.sh script
20 24
PATH=/usr/sbin:/usr/bin:/sbin:/bin
21
#CDM_HOME=/home/cdm
22
CDM_HOME=/opt/cdmserver
23
CDM_LIBRARY=$CDM_HOME".cdmLibrary"
24
DESC="CDM Server"
25 25
NAME=cdmserver
26
LOG="$CDM_HOME/$NAME.log"
27
DAEMON=/usr/bin/java
28
DAEMON_ARGS="-Xmx1024M -XX:PermSize=128m -XX:MaxPermSize=192m -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -Duser.home=$CDM_HOME -jar "$CDM_HOME/cdm-server.jar""
26
DESC="CDM Server"
27
CDM_HOME=/opt/cdmserver
28
CDM_USER="cdm"
29
CDM_GROUP="cdm"
30
LOGDIR="/var/log/$NAME"
31
JAVA=/usr/bin/java
32
DAEMON=/usr/bin/jsvc
33

  
34

  
35
CDMSERVER_PORT=8080
36
JAVA_ARGS="-Xmx1024M -XX:PermSize=128m -XX:MaxPermSize=192m -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -Duser.home=$CDM_HOME"
37
CDMSERVER_ARGS="-httpPort=$CDMSERVER_PORT -logdir=$LOGDIR"
38

  
39
# Define other required variables
29 40
PIDFILE=/var/run/$NAME.pid
30 41
SCRIPTNAME=/etc/init.d/$NAME
31 42

  
43
# Timeout in seconds for the shutdown of all webapps
44
CDMSERVER_SHUTDOWN=30
45

  
32 46
# Exit if the package is not installed
33 47
[ -x "$DAEMON" ] || exit 0
34 48

  
......
43 57
. /lib/lsb/init-functions
44 58

  
45 59

  
46
pid_of_cdmserver() {
47
    cat "$PIDFILE"
48
}
60
#### setting java home for JSVC ####
61

  
62
# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
63
# defined in /etc/default/jetty). Should contain a list of space separated directories.
64
JDK_DIRS="
65
	  /usr/lib/jvm/default-java \
66
	  /usr/lib/jvm/java-6-sun \
67
	  /usr/lib/jvm/java-6-openjdk \
68
	  /usr/lib/jvm/java-1.5.0-sun \
69
	  /usr/lib/jvm/java-gcj \
70
	  /usr/lib/j2sdk1.6-sun \
71
	  /usr/lib/j2sdk1.5-sun \
72
	  /usr/lib/j2sdk1.5-ibm \
73
	  /usr/lib/j2sdk1.4-sun \
74
	  /usr/lib/j2sdk1.4 \
75
	  /usr/lib/j2se/1.4 \
76
	  /usr/lib/kaffe/ \
77
	 "
78

  
79
# Look for the right JVM to use
80
for jdir in $JDK_DIRS; do
81
	if [ -d "$jdir" -a -z "${JAVA_HOME}" ]; then
82
		JAVA_HOME="$jdir"
83
	fi
84
done
85
export JAVA_HOME
86

  
87
#BOOTSTRAP_CLASS="eu.etaxonomy.cdm.server.Bootloader"
88
BOOTSTRAP_CLASS="eu.etaxonomy.cdm.server.jsvc.ServiceWrapper"
89
JSVC_CLASSPATH="/usr/share/java/commons-daemon.jar:$JAVA_HOME/lib/tools.jar:$CDM_HOME/cdm-server.jar"
90
JSVC_JVM="server"
91

  
92
# Check for JAVA_HOME
93
if [ -z "$JAVA_HOME" ]; then
94
	log_failure_msg "Could not start $DESC because no Java Development Kit"
95
	log_failure_msg "(JDK) was found. Please download and install JDK 1.4 or higher and set"
96
	log_failure_msg "JAVA_HOME in /etc/default/jetty to the JDK's installation directory."
97
	exit 0
98
fi
99

  
49 100

  
50 101
#
51 102
# Function that starts the daemon/service
......
57 108
	#   0 if daemon has been started
58 109
	#   1 if daemon was already running
59 110
	#   2 if daemon could not be started
60
	# log_daemon_msg "Starting $DESC" "$NAME with $DAEMON_ARGS"
61
	echo -ne "Starting $DESC" "$NAME with $DAEMON_ARGS"
62
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
63
		|| return 1
64
	start-stop-daemon --start --pidfile $PIDFILE --make-pidfile --background --exec $DAEMON -- \
65
		$DAEMON_ARGS \
66
		|| return 2
67
	# Add code here, if necessary, that waits for the process to be ready
68
	# to handle requests from services started subsequently which depend
69
	# on this one.  As a last resort, sleep for some time.
70
        #while { pid_of_cdmserver > /dev/null ; } &&
71
        #        ! { tail +$cnt "$LOG" | grep -q 'Winstone Servlet Engine .* running' ; } ; do
72
        #      sleep 1
73
        #done
111
	log_daemon_msg "Starting $DESC." "$NAME : java $JAVA_OPTIONS -jar $CDM_HOME/cdm-server.jar $CDMSERVER_ARGS"
112

  
113
	if start-stop-daemon --quiet --test --start --pidfile "$PIDFILE" \
114
				--user "$CDM_USER" --startas "$JAVA" > /dev/null
115
	then
116

  
117
		if [ -f $PIDFILE ] ; then
118
			log_warning_msg "$PIDFILE exists, but cdmserver was not running. Ignoring $PIDFILE"
119
		fi
120

  
121
		chown -R $CDM_USER:adm "$LOGDIR"
74 122

  
123
		echo $JSVC_CLASSPATH
124
		echo $JAVA_OPTIONS $BOOTSTRAP_CLASS  $CDMSERVER_ARGS
125

  
126
		$DAEMON -user "$CDM_USER" -jvm $JSVC_JVM -cp $JSVC_CLASSPATH \
127
		    -outfile $LOGDIR/out.log -errfile $LOGDIR/out.log \
128
		    -pidfile "$PIDFILE" $JAVA_OPTIONS $BOOTSTRAP_CLASS  $CDMSERVER_ARGS
129

  
130

  
131
		sleep 5
132
		if start-stop-daemon --test --start --pidfile "$PIDFILE" \
133
			--user $CDM_USER --startas "$JAVA" >/dev/null; then
134
			log_daemon_msg "$DESC did not start, please check ${LOGDIR}/out.log" "$NAME"
135
			return 2
136
		else
137
			log_daemon_msg "$DESC started, reachable on http://localhost:$CDMSERVER_PORT/." "$NAME"
138
			return 0
139
		fi
140

  
141

  
142
	else
143
		log_warning_msg "(already running)."
144
		exit 1
145
	fi
75 146
}
76 147

  
77 148
#
......
84 155
	#   1 if daemon was already stopped
85 156
	#   2 if daemon could not be stopped
86 157
	#   other if a failure occurred
87
	log_daemon_msg "Stopping $DESC" "$NAME"
88
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
89
	RETVAL="$?"
90
	[ "$RETVAL" = 2 ] && return 2
91
	# Wait for children to finish too if this is a daemon that forks
92
	# and if the daemon is only ever run from this initscript.
93
	# If the above conditions are not satisfied then add some other code
94
	# that waits for the process to drop all resources that could be
95
	# needed by services started subsequently.  A last resort is to
96
	# sleep for some time.
97
	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
98
	[ "$?" = 2 ] && return 2
99
	# Many daemons don't delete their pidfiles when they exit.
100
	rm -f $PIDFILE
158
	log_daemon_msg "Stopping $DESC." "$NAME"
159

  
160
	if start-stop-daemon --quiet --test --start --pidfile "$PIDFILE" \
161
				--user "$CDM_USER" --startas "$JAVA" > /dev/null
162
	then
163
		if [ -x "$PIDFILE" ]; then
164
			log_warning_msg "(not running but $PIDFILE exists)."
165
		else
166
			log_warning_msg "(not running)."
167
		fi
168
	else
169
		start-stop-daemon --quiet --stop \
170
			--pidfile $PIDFILE --user "$CDM_USER" --startas "$JAVA"  > /dev/null
171

  
172
		while ! start-stop-daemon --quiet --test --start \
173
			  --pidfile "$PIDFILE" --user "$CDM_USER" --startas "$JAVA" > /dev/null; do
174
			sleep 1
175
			log_progress_msg "."
176
			CDMSERVER_SHUTDOWN=`expr $CDMSERVER_SHUTDOWN - 1` || true
177
			if [ $CDMSERVER_SHUTDOWN -ge 0 ]; then
178
				start-stop-daemon --oknodo --quiet --stop \
179
					--pidfile "$PIDFILE" --user "$CDM_USER" \
180
					--startas "$JAVA"
181
			else
182
				log_progress_msg " (killing) "
183
				start-stop-daemon --stop --signal 9 --oknodo \
184
					--quiet --pidfile "$PIDFILE" \
185
					--user "$CDM_USER"
186
			fi
187
		done
188
		rm -f $PIDFILE
189
		log_daemon_msg "$DESC stopped." "$NAME"
190
	fi
101 191
	return "$RETVAL"
102 192
}
103 193

  
......
114 204
	return 0
115 205
}
116 206

  
207
report_status() {
208
	if start-stop-daemon --quiet --test --start --pidfile "$PIDFILE" \
209
		--user "$CDM_USER" --startas "$JAVA" > /dev/null; then
210

  
211
		if [ -f "$PIDFILE" ]; then
212
		    log_success_msg "$DESC is not running, but pid file exists."
213
			exit 1
214
		else
215
		    log_success_msg "$DESC is not running."
216
			exit 3
217
		fi
218
	else
219
		log_success_msg "$DESC is running with pid `cat $PIDFILE`, and is reachable on http://localhost:$CDMSERVER_PORT/"
220
	fi
221
}
222

  
117 223
case "$1" in
118
  start)
119
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
224

  
225
	start)
120 226
	do_start
121 227
	case "$?" in
122 228
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
123 229
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
124 230
	esac
125 231
	;;
126
  debug)
127
    DAEMON_ARGS=" -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 $DAEMON_ARGS"
128
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting in debug mode $DESC" "$NAME"
129
    do_start
130
    case "$?" in
131
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
132
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
133
    esac
134
    ;;
135
  stop)
136
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
232

  
233
	debug)
234
		JAVA_ARGS=" -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 $JAVA_ARGS"
235
		do_start
236
		case "$?" in
237
				0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
238
				2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
239
		esac
240
	;;
241

  
242
	stop)
137 243
	do_stop
138 244
	case "$?" in
139 245
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
140 246
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
141 247
	esac
142 248
	;;
143
  #reload|force-reload)
249

  
250
	#reload|force-reload)
144 251
	#
145 252
	# If do_reload() is not implemented then leave this commented out
146 253
	# and leave 'force-reload' as an alias for 'restart'.
......
149 256
	#do_reload
150 257
	#log_end_msg $?
151 258
	#;;
152
  restart|force-reload)
259

  
260
	restart|force-reload)
153 261
	#
154 262
	# If the "reload" option is implemented then remove the
155 263
	# 'force-reload' alias
......
157 265
	log_daemon_msg "Restarting $DESC" "$NAME"
158 266
	do_stop
159 267
	case "$?" in
160
	  0|1)
268
		0|1)
161 269
		do_start
162 270
		case "$?" in
163 271
			0) log_end_msg 0 ;;
......
165 273
			*) log_end_msg 1 ;; # Failed to start
166 274
		esac
167 275
		;;
168
	  *)
169
	  	# Failed to stop
276
		*)
277
		# Failed to stop
170 278
		log_end_msg 1
171 279
		;;
172 280
	esac
173 281
	;;
174
  *)
282

  
283
	status)
284
		report_status
285
		log_end_msg 0
286
	;;
287

  
288
	*)
175 289
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
176
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|debug}" >&2
290
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status|debug}" >&2
177 291
	exit 3
178 292
	;;
179 293
esac

Also available in: Unified diff