This script can be used as a Template for developing a customized service script, which can be controlled using 'chkconfig' and 'service' command. In this script, I have converted an application ('admin') startup/stop/restart/check status procedure into a chkconfig script. This 'admin' application is a JBoss application which uses JDK version 1.6.0_14.
______________________________________________________________________
#!/bin/bash
#
# chkconfig: 2345 9 91
# description: Admin application
#
# Source function library.
if [ -x /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi
PROG=`basename $0`
# Initializing Return value
RETVAL=0
# Defining Application start script path
START_APP_PATH='/home/jboss/startChain.sh'
# Defining JDK path
JDK_PATH='/usr/java/jdk1.6.0_14/bin/java'
# Unique string for Jboss process
UNIQUE_STRG='Dprogram.name=run.sh'
# Defining the pid file for this jboss instance
JBOSSPID=`ps -eaf | grep java | grep "$UNIQUE_STRG" | grep -v grep | awk '{print $2}'`
#define the lock file for this jboss instance
JBOSSLOCK="/var/lock/subsys/${PROG}"
# Current time
CURRENT_TIME=`date '+%m-%d-%y %k:%M'`
# Set the defaults.
LOGFILE=/var/log/${PROG}/${PROG}-`date '+%m-%d-%y'`.log
start ()
{
echo -n "Checking the current application status........."
status
if [ "$?" = 0 ]; then
echo -e "Attempting to start Admin Application\n"
touch $JBOSSLOCK
. $START_APP_PATH
RETVAL=$?
if [ "$RETVAL" == "0" ]; then
sleep 10
JBOSSPID=`ps -eaf | grep java | grep "$UNIQUE_STRG" | grep -v grep | awk '{print $2}'`
echo "Admin application successfully started with JBOSS PID: $JBOSSPID" && echo
echo "$CURRENT_TIME: Admin application started" >> $LOGFILE
else
echo "Couldn't start Admin application, Please check it manually" && echo
echo "$CURRENT_TIME: Couldn't start Admin application." >> $LOGFILE
fi
else
echo "$CURRENT_TIME: Admin application already running. Exiting the script...!" >> $LOGFILE
echo "Admin application already running. Exiting the script...!" && exit 0
fi
}
stop ()
{
echo -n "Checking the current application status........."
status
if [ "$?" = "1" ]; then
JBOSSPID=`ps -eaf | grep java | grep "$UNIQUE_STRG" | grep -v grep | awk '{print $2}'`
echo -e "Attempting to stop Admin application with JBOSS PID: $JBOSSPID\n"
kill -15 "$JBOSSPID"
sleep 2
CNT=1
while true; do
JBOSS_PROC_CNT=`ps -ef | grep -c "$JBOSSPID"`
if [ "$JBOSS_PROC_CNT" == "0" ]; then
echo && echo "Admin Application has been stopped" && echo
rm -rf /var/lock/subsys/admin
echo "$CURRENT_TIME: Admin application has been stopped." >> $LOGFILE
JBOSSPID=''
return 0
fi
CNT=`expr $CNT + 1`
if [ $CNT -gt 28 ]; then
echo "Couldn't stop Admin Application. Please check it manually." && echo
echo "$CURRENT_TIME: Couldn't stop Admin Application. Please check it manually." >> $LOGFILE
return 0
fi
sleep 1
echo -n "."
done
fi
}
status ()
{
if [ -z "$JBOSSPID" ] && [ "$JBOSSPID" = '' ]; then
echo "Admin application isn't running" && echo
return 0
else
echo "Admin application is running with JBOSS PID: $JBOSSPID" && echo
return 1
fi
}
restart ()
{
status
if [ "$?" = 0 ]; then
start
else
# Stoping the Application
stop
#Restarting Application
start
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
echo "Restarting Admin application" && echo
restart
;;
*)
echo "Usage: $PROG {start|stop|status|restart}"
exit 1
esac
exit $RETVAL
SAMPLE EXECUTION
[root@hostxyz-t3 tmp]# chkconfig --list | grep admin
admin 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@hostxyz-t3 tmp]# su - jboss
[jboss@hostxyz-t3 ~]$ sudo /sbin/service admin status
Admin application is running with JBOSS PID: 15912
[jboss@hostxyz-t3 ~]$ sudo /sbin/service admin stop
Checking the current application status.........Admin application is running with JBOSS PID: 15912
Attempting to stop Admin application with JBOSS PID: 15912
..........
Admin Application has been stopped
[jboss@hostxyz-t3 ~]$ sudo /sbin/service admin status
Admin application isn't running
[jboss@hostxyz-t3 ~]$ sudo /sbin/service admin start
Checking the current application status.........Admin application isn't running
Attempting to start Admin Application
Admin application successfully started with JBOSS PID: 12719
[jboss@hostxyz-t3 ~]$ sudo /sbin/service admin status
Admin application is running with JBOSS PID: 12719
[jboss@hostxyz-t3 ~]$ sudo /sbin/service admin restart
Restarting Admin application
Admin application is running with JBOSS PID: 12719
Checking the current application status.........Admin application is running with JBOSS PID: 12719
Attempting to stop Admin application with JBOSS PID: 12719
.
Admin Application has been stopped
Checking the current application status.........Admin application isn't running
Attempting to start Admin Application
Admin application successfully started with JBOSS PID: 12909
[jboss@hostxyz-t3 ~]$
No comments:
Post a Comment