openfire 4.91 KB
Newer Older
1 2
#!/bin/sh
#
Daniel Henninger's avatar
Daniel Henninger committed
3
# openfire	Stops and starts the Openfire XMPP service.
4 5 6 7
#
# chkconfig: 2345 99 1
# description: Openfire is an XMPP server, which is a server that facilitates \
#              XML based communication, such as chat.
Daniel Henninger's avatar
Daniel Henninger committed
8
# config: /opt/openfire/conf/openfire.xml
Daniel Henninger's avatar
Daniel Henninger committed
9
# config: /etc/sysconfig/openfire
10 11
# pidfile: /var/run/openfire.pid
# 
12 13
# This script has currently been tested on Redhat, CentOS, and Fedora  based
# systems.
14 15
#

16 17 18 19 20 21
#####
# Begin setup work
#####

# Initialization
PATH="/sbin:/bin:/usr/bin:/usr/sbin"
22 23
RETVAL=0

24
# Check that we are root ... so non-root users stop here.
25 26 27 28
if [ "`id -u`" != 0 ]; then
	echo $0 must be run as root
	exit 1
fi
29 30

# Get config.
Daniel Henninger's avatar
Daniel Henninger committed
31
[ -f "/etc/sysconfig/openfire" ] && . /etc/sysconfig/openfire
32 33 34 35
if [ -f "/etc/init.d/functions" ]; then
  FUNCTIONS_FOUND=true
  . /etc/init.d/functions
fi
36

37 38
# If openfire user is not set in sysconfig, set to daemon.
[ -z "$OPENFIRE_USER" ] && OPENFIRE_USER="daemon"
39

Daniel Henninger's avatar
Daniel Henninger committed
40
# If pid file path is not set in sysconfig, set to /var/run/openfire.pid.
Daniel Henninger's avatar
Daniel Henninger committed
41
[ -z "$OPENFIRE_PIDFILE" ] && OPENFIRE_PIDFILE="/var/run/openfire.pid"
42 43 44

# -----------------------------------------------------------------

45 46
# If a openfire home variable has not been specified, try to determine it.
if [ -z "$OPENFIRE_HOME" -o ! -d "$OPENFIRE_HOME" ]; then
Daniel Henninger's avatar
Daniel Henninger committed
47 48 49
	if [ -d "/usr/share/openfire" ]; then
		OPENFIRE_HOME="/usr/share/openfire"
	elif [ -d "/usr/local/openfire" ]; then
50
		OPENFIRE_HOME="/usr/local/openfire"
Daniel Henninger's avatar
Daniel Henninger committed
51 52
	elif [ -d "/opt/openfire" ]; then
		OPENFIRE_HOME="/opt/openfire"
53
	else
Daniel Henninger's avatar
Daniel Henninger committed
54
		echo "Could not find Openfire installation under /opt, /usr/share, or /usr/local."
55
		echo "Please specify the Openfire installation location as variable OPENFIRE_HOME"
56
		echo "in /etc/sysconfig/openfire."
57 58 59 60
		exit 1
	fi
fi

61 62 63 64 65 66 67
# If log path is not set in sysconfig, set to $OPENFIRE_HOME/logs.
[ -z "$OPENFIRE_LOGDIR" ] && OPENFIRE_LOGDIR="${OPENFIRE_HOME}/logs"

# Attempt to locate java installation.
if [ -z "$JAVA_HOME" ]; then
	if [ -d "${OPENFIRE_HOME}/jre" ]; then
		JAVA_HOME="${OPENFIRE_HOME}/jre"
Daniel Henninger's avatar
Daniel Henninger committed
68
	elif [ -d "/etc/alternatives/jre" ]; then
Daniel Henninger's avatar
Daniel Henninger committed
69
		JAVA_HOME="/etc/alternatives/jre"
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	else
		jdks=`ls -r1d /usr/java/j*`
		for jdk in $jdks; do
			if [ -f "${jdk}/bin/java" ]; then
				JAVA_HOME="$jdk"
				break
			fi
		done
	fi
fi
JAVACMD="${JAVA_HOME}/bin/java"

if [ ! -d "$JAVA_HOME" -o ! -x "$JAVACMD" ]; then
	echo "Error: JAVA_HOME is not defined correctly."
	echo "       Can not sure execute $JAVACMD."
	exit 1
fi

# Prepare location of openfire libraries
OPENFIRE_LIB="${OPENFIRE_HOME}/lib"

# Prepare openfire command line
OPENFIRE_OPTS="${OPENFIRE_OPTS} -DopenfireHome=${OPENFIRE_HOME} -Dopenfire.lib.dir=${OPENFIRE_LIB}"

# Prepare local java class path
if [ -z "$LOCALCLASSPATH" ]; then
	LOCALCLASSPATH="${OPENFIRE_LIB}/startup.jar"
else
	LOCALCLASSPATH="${OPENFIRE_LIB}/startup.jar:${LOCALCLASSPATH}"
fi

# Export any necessary variables
export JAVA_HOME JAVACMD

# Lastly, prepare the full command that we are going to run.
OPENFIRE_RUN_CMD="${JAVACMD} -server ${OPENFIRE_OPTS} -classpath \"${LOCALCLASSPATH}\" -jar \"${OPENFIRE_LIB}/startup.jar\""

#####
# End setup work
#####

111 112 113 114 115 116


start() {
	OLD_PWD=`pwd`
	cd $OPENFIRE_LOGDIR

117 118 119 120 121 122 123 124 125
	PID=$(findPID)
	if [ -n "$PID" ]; then                                                
	    echo "Openfire is already running."                                 
	    RETVAL=1                                                           
	    return                                                             
	fi                                                                     

	# Start daemons.                                                       
	echo -n "Starting openfire: "                                          
126

127 128
	rm -f nohup.out
	su -s /bin/sh -c "nohup $OPENFIRE_RUN_CMD > $OPENFIRE_LOGDIR/nohup.out 2>&1 &" $OPENFIRE_USER
129 130 131 132
	RETVAL=$?

	echo

Daniel Henninger's avatar
Daniel Henninger committed
133
	[ $RETVAL -eq 0 -a -d /var/lock/subsys ] && touch /var/lock/subsys/openfire
134 135

	sleep 1 # allows prompt to return
136 137 138 139

	PID=$(findPID)
	echo $PID > $OPENFIRE_PIDFILE

140 141 142 143 144
	cd $OLD_PWD
}

stop() {
	# Stop daemons.
145
	echo -n "Shutting down openfire: "
146

147 148 149 150 151 152
	if [ -f "$OPENFIRE_PIDFILE" ]; then
		killproc -p $OPENFIRE_PIDFILE -d 10
		rm -f $OPENFIRE_PIDFILE
	else
		PID=$(findPID)
		if [ -n $PID ]; then
153
			kill $PID
154 155
		else
			echo "Openfire is not running."
156 157 158
		fi
	fi
	
159 160 161
	RETVAL=$?
	echo

Daniel Henninger's avatar
Daniel Henninger committed
162
	[ $RETVAL -eq 0 -a -f "/var/lock/subsys/openfire" ] && rm -f /var/lock/subsys/openfire
163 164 165 166 167 168 169 170 171
}

restart() {
	stop
	sleep 10 # give it a few moments to shut down
	start
}

condrestart() {
Daniel Henninger's avatar
Daniel Henninger committed
172
	[ -e "/var/lock/subsys/openfire" ] && restart
173 174 175 176
	return 0
}

status() {
177 178 179 180
	PID=$(findPID)
	if [ -n "$PID" ]; then
		echo "openfire is running"
		RETVAL=0
181
	else 
182 183
		echo "openfire is not running"
		RETVAL=1
184 185 186
	fi
}

187 188 189
findPID() {
	echo `ps ax --width=1000 | grep openfire | grep startup.jar | awk '{print $1}'`
}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

# Handle how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	condrestart)
		condrestart
		;;
	status) 
		status
		;;
	*)
209
		echo "Usage $0 {start|stop|restart|status|condrestart}"
210 211 212 213
		RETVAL=1
esac

exit $RETVAL