Skip to content

Instantly share code, notes, and snippets.

@warmans
Created January 27, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save warmans/03e8ebd178b5e94fd7c7 to your computer and use it in GitHub Desktop.
Save warmans/03e8ebd178b5e94fd7c7 to your computer and use it in GitHub Desktop.
Centos jstatd init script
#!/bin/sh
# jstatd - runs the jstat daemon
#
# chkconfig: - 85 15
# description: Jstatd JVM monitoring
# processname: jstatd
# pidfile: /var/run/jstatd.pid
# Source function library.
. /etc/rc.d/init.d/functions
USER=root
PID_FILE=/var/run/jstatd.pid
BIND_INTERFACE=eth0
BIND_IP=$(ifconfig $BIND_INTERFACE | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
PROGNAME=`basename $0`
if [ "$JAVA_HOME" == "" ]; then
JAVA_HOME=/usr/java/latest
fi
if [ "$JSTATD_PORT" == "" ]; then
JSTATD_PORT=1099
fi
if [ "$JSTATD_BIN" == "" ]; then
if [ "$JAVA_HOME" != "" ]; then
JSTATD_BIN=$JAVA_HOME/bin/jstatd
else
JSTATD_BIN=jstatd
fi
fi
if ! command -v ${JSTATD_BIN} >/dev/null 2>&1; then
echo "${JSTATD_BIN} not found or executable"
exit 1
fi
if [ "$POLICY_FILE" == "" ]; then
POLICY_FILE=/tmp/.jstatd.all.policy
fi
#if no policy is specifed create one
if [ ! -r ${POLICY_FILE} ]; then
cat >${POLICY_FILE} <<'EOF'
grant codebase "file:${java.home}/../lib/tools.jar" {
permission java.security.AllPermission;
};
EOF
fi
start() {
if test -f $PID_FILE
then
PID=`cat $PID_FILE`
if ps --pid $PID >/dev/null;
then
echo "$PROGNAME is already running: $PID"
exit 0
else
echo "Removing stale pid file: $PID_FILE"
fi
fi
echo -n "Starting $PROGNAME..."
su $USER -c "${JSTATD_BIN} -J-DJPS_APP_ID=${JPS_APP_ID} -p $JSTATD_PORT -J-Djava.security.policy=${POLICY_FILE} -J-Djava.rmi.server.hostname=$BIND_IP -J-Djava.net.preferIPv4Stack=true &"
if [ $? == "0" ]; then
success
else
failure
fi
echo
ps -U $USER -C java -o pid,cmd | grep $JSTATD_BIN | awk {'print $1 '} > $PID_FILE
}
stop() {
if test -f $PID_FILE
then
echo -n "Stopping $PROGNAME..."
PID=`cat $PID_FILE`
su $USER -c "kill -3 $PID"
if kill $PID ;
then
sleep 2
test -f $PID_FILE && rm -f $PID_FILE
success
else
echo "$PROGNAME could not be stopped..."
failure
fi
else
echo "$PROGNAME is not running."
failure
fi
echo
}
status() {
if test -f $PID_FILE;
then
PID=`cat $PID_FILE`
if [ -z "$PID" ]; then
echo "$PROGNAME isn't running..."
exit 1
else
echo "$PROGNAME is running...$PID"
exit 0
fi
fi
exit 1
}
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
stop
start
;;
status)
$1
;;
*)
echo "Usage: $SELF start|stop|restart|status"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment