This is how I configured the JavaMelody Collect Server to start as a service in CentOS 6. The collect server is deployed as a WAR with its embedded server.
JavaMelody:
https://github.com/javamelody/javamelody/wiki
JavaMelody Collect Server:
https://github.com/javamelody/javamelody/wiki/UserGuideAdvanced#optional-centralization-server-setup
Directory structure:
- /opt/javamelody
- javamelody-X.X.X.war
- javamelody.war (symbolic link to the previous war)
- conf
- javamelody.sh (launch script)
- work
- tmp (java temp directory for de process)
- storage (storage directory for monitor info)
First, create the
directoy structure:
mkdir /opt/javamelody
cp javamelody-1.63.0.war /opt/javamelody/
ln -s /opt/javamelody/javamelody-1.63.0.war /opt/javamelody/javamelody.war
mkdir /opt/javamelody/work
mkdir /opt/javamelody/work/tmp
mkdir /opt/javamelody/work/storage
mkdir /opt/javamelody/conf
In /opt/javamelody/conf you have to create the launching script, called
javamelody.sh:
#!/bin/sh
#
# JavaMelody script
# 09/03/2017
#
# https://github.com/javamelody/javamelody/wiki/UserGuideAdvanced#3-simpler-alternative-of-deployment-of-the-webapp-of-monitoring
#
# Params:
# - JMELODY_CONSOLE_LOG : Log directory
# - JMELODY_PIDFILE : File where the pid is stored
#
#
JAVA_OPTS="-server -Xmx256m"
JAVA_OPTS="$JAVA_OPTS -Djava.io.tmpdir=/opt/javamelody/work/tmp"
JAVA_OPTS="$JAVA_OPTS -Djavamelody.storage-directory=/opt/javamelody/work/storage"
JAVA_OPTS="$JAVA_OPTS -Djavamelody.authorized-users=sisifo:sisifo"
JMELODY_WAR=/opt/javamelody/javamelody.war
JMELODY_OPTS="--httpPort=9080"
# Run and store the PID
nohup java $JAVA_OPTS -jar $JMELODY_WAR $JMELODY_OPTS 0</dev/null 2>&1 >> $JMELODY_CONSOLE_LOG &
JMELODY_PID=$!
echo $JMELODY_PID > $JMELODY_PIDFILE
JavaMelody will be executed with its own
user, called 'javamelody':
useradd -U javamelody
chown -Rf javamelody:javamelody /opt/javamelody/
Now, you have to create the service script called
javamelody in /etc/init.d:
#!/bin/sh
#
# JavaMeoldy Collect Server script
#
# Source function library.
. /etc/init.d/functions
JMELODY_SCRIPT=/opt/javamelody/conf/javamelody.sh
JMELODY_CONSOLE_LOG=/var/log/javamelody/javamelody.log
JMELODY_PIDFILE=/var/run/javamelody.pid
JMELODY_USER=javamelody
prog='JavaMelody Collect Server'
# Set defaults.
if [ -z "$JMELODY_PIDFILE" ]; then
JMELODY_PIDFILE=/var/run/javamelody.pid
fi
export JMELODY_PIDFILE
if [ -z "$JMELODY_CONSOLE_LOG" ]; then
JMELODY_CONSOLE_LOG=/var/log/javamelody/javamelody.log
fi
if [ -z "$STARTUP_WAIT" ]; then
STARTUP_WAIT=30
fi
if [ -z "$SHUTDOWN_WAIT" ]; then
SHUTDOWN_WAIT=30
fi
start() {
echo -n "Starting $prog: "
if [ -f $JMELODY_PIDFILE ]; then
read ppid < $JMELODY_PIDFILE
if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
echo -n "$prog is already running"
failure
echo
return 1
else
rm -f $JMELODY_PIDFILE
fi
fi
mkdir -p $(dirname $JMELODY_CONSOLE_LOG)
cat /dev/null > $JMELODY_CONSOLE_LOG
chown -Rf $JMELODY_USER $(dirname $JMELODY_CONSOLE_LOG) || true
mkdir -p $(dirname $JMELODY_PIDFILE)
chown $JMELODY_USER $(dirname $JMELODY_PIDFILE) || true
daemon --user $JMELODY_USER --pidfile $JMELODY_PIDFILE JMELODY_PIDFILE=$JMELODY_PIDFILE JMELODY_CONSOLE_LOG=$JMELODY_CONSOLE_LOG $JMELODY_SCRIPT
count=0
launched=false
until [ $count -gt $STARTUP_WAIT ]
do
grep 'Winstone Servlet * Engine' $JMELODY_CONSOLE_LOG > /dev/null
if [ $? -eq 0 ] ; then
launched=true
break
fi
sleep 1
let count=$count+1;
done
success
echo
return 0
}
stop() {
echo -n $"Stopping $prog: "
count=0;
if [ -f $JMELODY_PIDFILE ]; then
read kpid < $JMELODY_PIDFILE
let kwait=$SHUTDOWN_WAIT
# Try issuing SIGTERM
kill -15 $kpid
until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
do
sleep 1
let count=$count+1;
done
if [ $count -gt $kwait ]; then
kill -9 $kpid
fi
fi
rm -f $JMELODY_PIDFILE
success
echo
}
status() {
if [ -f $JMELODY_PIDFILE ]; then
read ppid < $JMELODY_PIDFILE
if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
echo "$prog is running (pid $ppid)"
return 0
else
echo "$prog dead but pid file exists"
return 1
fi
fi
echo "$prog is not running"
return 3
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
$0 start
;;
status)
status
;;
*)
## If no parameters are given, print which are avaiable.
echo "Usage: $0 {start|stop|status|restart|reload}"
exit 1
;;
esac
Finally, just configure it as an
automatic service:
chmod +x /etc/init.d/javamelody
chkconfig --add javamelody
chkconfig javamelody on
service javamelody start