blob: b7df83b18419500da4693a0ec2a3f789e8fd0499 [file] [log] [blame]
#!/bin/sh
### BEGIN INIT INFO
# Provides: ofbiz
# Required-Start: $local_fs $remote_fs $network $time
# Required-Stop: $local_fs $remote_fs $network $time
# Should-Start: postgresql mysql exim4
# Should-Stop: postgresql mysql exim4
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: OfBiz container
### END INIT INFO
#Licensed to the Apache Software Foundation (ASF) under one
#or more contributor license agreements. See the NOTICE file
#distributed with this work for additional information
#regarding copyright ownership. The ASF licenses this file
#to you under the Apache License, Version 2.0 (the
#"License"); you may not use this file except in compliance
#with the License. You may obtain a copy of the License at
#
#http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied. See the License for the
#specific language governing permissions and limitations
#under the License.
set -e
if ! [ -e "/usr/share/ofbiz/ofbiz.jar" ]; then
exit 0
fi
PATH=/bin:/usr/bin:/sbin:/usr/sbin
# set defaults
DEFAULT=/etc/default/ofbiz
JAVA_HOME=""
JAVA_HOMES="/usr/lib/jvm/java-6-sun"
MEMIF=""
UMASK=002
. /lib/lsb/init-functions
if [ -e /etc/default/rcS ]; then
. /etc/default/rcS
fi
if [ -e /etc/default/locale ]; then
. /etc/default/locale
export LANG
fi
# allow local admin to change defaults
if [ -e "$DEFAULT" ]; then
. "$DEFAULT"
fi
umask $UMASK
PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=ofbiz
DESC="OfBiz Container"
OFBIZ_HOME=/usr/share/ofbiz
USER=ofbiz
PIDFILE=/var/run/ofbiz.pid
CLASS=org.ofbiz.base.start.Start
PROPS=""
if [ -f "/etc/ofbiz/debconf.cfg" ]; then
PROPS="$PROPS -Dofbiz.system.props=/etc/ofbiz/debconf.cfg"
fi
if [ "x$JAVA_HOME" = x ]; then
if [ "x$JAVA_HOMES" != x ]; then
for possible in $JAVA_HOMES; do
if [ -e "$possible/bin/java" ]; then
JAVA_HOME="$possible"
break
fi
done
fi
if [ "x$JAVA_HOME" = x ]; then
log_failure_msg "Couldn't find a java."
if [ "x$JAVA_HOMES" != x ]; then
log_failure_msg "Tried $JAVA_HOMES"
fi
exit 1
fi
fi
JAVA="$JAVA_HOME/bin/java"
CLASSPATH="$OFBIZ_HOME/ofbiz.jar:/usr/share/java/commons-daemon.jar"
run_ofbiz_cmd() {
(
cd "$OFBIZ_HOME"
"$JAVA" $PROPS -jar ofbiz.jar "$1"
)
}
if [ -z "$(which jsvc)" ]; then
jsvc() {
jsvc_jvm=""
jsvc_wait=""
jsvc_verbose_class=""
jsvc_verbose_gc=""
jsvc_verbose_jni=""
jsvc_pidfile=""
jsvc_detach=1
jsvc_user=""
jsvc_outfile="/dev/null"
jsvc_errfile="/dev/null"
jsvc_home=""
jsvc_cp=""
jsvc_extra=""
jsvc_stop=""
while [ $# -gt 0 ]; do
case "$1" in
(-version) ;;
(-help) ;;
(-?) ;;
(-check) ;;
(-jvm) jsvc_jvm="$2"; shift;;
(-wait) jsvc_wait="$2"; shift;;
(-stop) jsvc_stop=1;;
(-verbose:class) jsvc_verbose_class=1; shift;;
(-verbose:gc) jsvc_verbose_gc=1; shift;;
(-verbose:jni) jsvc_verbose_jni=1; shift;;
(-pidfile) jsvc_pidfile="$2"; shift;;
(-nodetach) jsvc_detach="";;
(-user) jsvc_user="$2"; shift;;
(-outfile) jsvc_outfile="$2"; shift;;
(-errfile) jsvc_errfile="$2"; shift;;
(-home) jsvc_home="$2"; shift;;
(-classpath) jsvc_cp="$2"; shift;;
(-cp) jsvc_cp="$2"; shift;;
(-D*) jsvc_extra="$jsvc_extra \"$1\"";;
(-X*) jsvc_extra="$jsvc_extra \"$1\"";;
(*) break;;
esac
shift
done
if ! [ "$jsvc_home" ]; then log_failure_msg "-home not set"; exit 1; fi
if ! [ "$jsvc_cp" ]; then log_failure_msg "-cp not set"; exit 1; fi
if [ "$jsvc_jvm" ]; then log_failure_msg "-jvm not supported"; exit 1; fi
if [ "$jsvc_wait" ]; then log_failure_msg "-wait not supported"; exit 1; fi
if [ "$jsvc_stop" ]; then log_failure_msg "-stop not supported"; exit 1; fi
jsvc_args=""
if [ "$jsvc_verbose_class" ]; then :; fi
if [ "$jsvc_verbose_gc" ]; then :; fi
if [ "$jsvc_verbose_jni" ]; then :; fi
jsvc_command_file=$(mktemp)
echo "#!/bin/sh" > "$jsvc_command_file"
chmod +x "$jsvc_command_file"
echo "rm -f \"$jsvc_command_file\"" >> "$jsvc_command_file"
if [ "$jsvc_pidfile" ]; then
touch "$jsvc_pidfile"
if [ "$jsvc_user" ]; then
chown "$jsvc_user" "$jsvc_pidfile"
fi
echo "echo \$\$ > \"$jsvc_pidfile\"" >> "$jsvc_command_file"
fi
echo "exec \"$JAVA\" -classpath \"$jsvc_cp\" $jsvc_extra $@" >> "$jsvc_command_file"
if [ "$jsvc_user" ]; then
chown "$jsvc_user" "$jsvc_command_file"
jsvc_command="su -c \"$jsvc_command_file\" -s \"/bin/sh\" \"$jsvc_user\""
else
jsvc_command="$jsvc_command_file"
fi
jsvc_command="$jsvc_command 1>\"$jsvc_outfile\" 2>\"$jsvc_errfile\""
if [ "$jsvc_detach" ]; then
eval $jsvc_command &
else
trap "rm -f \"$jsvc_command_file\"" EXIT
eval $jsvc_command
fi
return $?
}
fi
get_status() {
status="$(run_ofbiz_cmd -status 2>/dev/null | sed -n 's/^Current Status : //p')"
case "$status" in
(Running|Starting|Stopping|Not\ Running)
echo $status
;;
(*)
echo Not Running
;;
esac
}
wait_for_status() {
count="$1"
while :; do
if [ "$(get_status)" = "$2" ]; then
break;
fi
count=$(($count - 1))
if [ "$count" -eq 0 ]; then
log_failure_msg "Timeout waiting for $3"
exit 1
fi
log_begin_msg "."
sleep 1
done
}
ofbiz_shutdown() {
run_ofbiz_cmd -shutdown >/dev/null
}
wait_for_stop() {
wait_for_status 30 "Not Running" "stop"
count="10"
while :; do
if ! start-stop-daemon --pidfile "$PIDFILE" --exec /usr/bin/jsvc --user $USER --signal 0 --quiet --stop; then
break
fi
count=$(($count - 1))
if [ "$count" -eq 0 ]; then
log_failure_msg "Timeout waiting for stop"
exit 1
fi
log_begin_msg "."
sleep 1
done
}
start() {
case "$(get_status)" in
(Stopping)
log_progress_msg "(waiting for stop"
wait_for_stop
log_progress_msg ")"
;;
(Not\ Running)
;;
(Starting|Running)
log_progress_msg "(already running)"
return
;;
(*)
log_progress_msg "(forcing stop"
ofbiz_shutdown
wait_for_stop
log_progress_msg ")"
;;
esac
(
cd "$OFBIZ_HOME"
touch /var/log/ofbiz/ofbiz.log
chown ofbiz.ofbiz /var/log/ofbiz/ofbiz.log
jsvc -pidfile "$PIDFILE" \
-user $USER \
-outfile /var/log/ofbiz/console.log -errfile '&1' \
-home "$JAVA_HOME" -cp "$CLASSPATH" \
$PROPS \
$MEMIF \
$CLASS
)
wait_for_status 120 "Running" "start"
}
stop() {
case "$(get_status)" in
(Not\ Running)
;;
(*)
ofbiz_shutdown
;;
esac
wait_for_stop
if [ -e "$PIDFILE" ]; then
rm -f "$PIDFILE"
fi
}
install() {
case "$(get_status)" in
(Stopping)
log_progress_msg "(waiting for stop"
wait_for_stop
log_progress_msg ")"
;;
(Not\ Running)
;;
(Starting|Running)
log_failure_msg "(running, can't install)"
exit 1
;;
esac
(
cd "$OFBIZ_HOME"
touch /var/log/ofbiz/ofbiz.log
chown ofbiz.ofbiz /var/log/ofbiz/ofbiz.log
jsvc -pidfile "$PIDFILE" \
-nodetach \
-user $USER \
-outfile /dev/fd/3 -errfile '&1' \
-home "$JAVA_HOME" -cp "$CLASSPATH" \
$PROPS \
$MEMIF \
$CLASS \
install "$@" 3>&1 | \
tee -a /var/log/ofbiz/install.log
)
}
module_hook() {
files=""
result="0"
while [ $# -gt 0 ]; do
case "$1" in
(-file=*)
file="$(echo "$1" | sed 's/^-file=//')"
if ! [ -e "$file" ]; then
log_failure_msg "File $file does not exist"
result=1
else
files="$files,$file"
fi
;;
(-file)
file="$2"
if ! shift; then
log_failure_msg "No file specified"
result=1
elif ! [ -e "$file" ]; then
log_failure_msg "File $file does not exist"
result=1
else
files="$files,$file"
fi
;;
(*)
log_failure_msg "Unknown argument($1)"
exit 1
;;
esac
shift
done
if ! [ "$files" ]; then
return 0
elif [ "$result" eq 1 ]; then
return 1
fi
files="$(echo "$files" | cut -f 2- -d ,)"
was_running=""
case "$(get_status)" in
(Not\ Running)
;;
(Stopping)
wait_for_stop
;;
(*)
log_daemon_msg "Stopping $DESC" "$NAME"
ofbiz_shutdown
wait_for_stop
log_end_msg 0
was_running=1
;;
esac
cd "$OFBIZ_HOME"
if ! jsvc \
-user $USER -outfile /dev/tty -errfile '&1' -nodetach \
-home "$JAVA_HOME" -cp "$CLASSPATH" \
$CLASS install -file="$files"; then
log_failure_msg "Error installing files"
result=1
fi
if [ "$was_running" ]; then
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg 0
fi
return "$result"
}
cmd="$1"
shift || true
case "$cmd" in
(start)
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg 0
;;
(stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
log_end_msg 0
;;
(install)
log_daemon_msg "Installing $DESC" "$NAME"
install "$@"
log_end_msg 0
;;
(reload|restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
stop
start
log_end_msg 0
;;
(module-hook)
if ! module_hook "$@"; then
exit 1
fi
;;
(*)
log_success_msg "Usage: $0 {start|stop|install|reload|force-reload|restart"
exit 1
;;
esac