May 27

Redhat run level and how to make tomcat service to run on boot time in CentOS

CentOS Comments Off on Redhat run level and how to make tomcat service to run on boot time in CentOS

Just today one of my friend are pm me and ask how to make his tomcat service at /etc/init.d/tomcat to run when the server reboot. At the first time i just tell him to use chkconfig because the os that he use was CentOS

What is chkconfig ?

Chkconfig is a basic system utility. It updates and queries runlevelinformation for system services. Chkconfig manipulates the numerous symbolic links in /etc/rc*.d, to relieve system administrators of some of the drudgery of manually editing the symbolic links.

chkconfig –list [name]
chkconfig –add name
chkconfig –del name
chkconfig [–level levels] name <on|off|reset>
chkconfig [–level levels] name

But when my friend use chkconfig there were error :

# chkconfig –levels 235 tomcat on
service tomcat does not support chkconfig

okay my friend ask me what is the 235 means , 235 is runlevel that the script starts.
For instance, if we called this script /etc/init.d/new-service and ran chkconfig new-service on, it would be active in runlevels 2,3 and 5.

0-6 are runlevels. Typically for Redhat based distributions:
0 – Halt the system
1 – Single-user mode
2 – Multi-user mode (without networking)
3 – Multi-user mode
5 – Multi-user mode, graphical login
6 – Reboot the system

you can check your service run level by using chkconfig –list:

# chkconfig –list
courier-authlib  0:off   1:off   2:on    3:on    4:on    5:on    6:off
courier-imap      0:off   1:off   2:on    3:on    4:on    5:on    6:off
crond                     0:off   1:off   2:on    3:on    4:on    5:on    6:off
httpd                      0:off   1:off   2:on    3:on    4:on    5:on    6:off
iptables                 0:off   1:off   2:on    3:on    4:on    5:on    6:off
ldap                         0:off   1:off   2:off   3:off   4:off     5:off   6:off
lm_sensors          0:off   1:off   2:on    3:on    4:on    5:on    6:off
lsws                         0:off   1:off   2:on    3:on    4:off   5:on    6:off
mcstrans               0:off   1:off   2:on    3:on    4:on    5:on    6:off
messagebus          0:off   1:off   2:off   3:on    4:on    5:on    6:off
mysqld                   0:off   1:off   2:off   3:off   4:off   5:off   6:off
named                    0:off   1:off   2:off   3:off   4:off   5:off   6:off
netconsole           0:off   1:off   2:off   3:off   4:off   5:off   6:off
netfs                        0:off   1:off   2:off   3:off   4:off   5:off   6:off
netplugd               0:off   1:off   2:off   3:off   4:off   5:off   6:off
network                0:off   1:off   2:on    3:on   4:on   5:on   6:off
portmap               0:off   1:off   2:off   3:off   4:off   5:off   6:off
rdisc                       0:off   1:off   2:off   3:off   4:off   5:off   6:off
restorecond        0:off   1:off   2:on    3:on    4:on  5:on    6:off
snmpd                   0:off   1:off   2:off   3:off   4:off   5:off   6:off
snmptrapd          0:off   1:off   2:off   3:off   4:off   5:off   6:off
sshd                       0:off   1:off   2:on    3:on    4:on    5:on    6:off
syslog                   0:off   1:off   2:on    3:on    4:on    5:on    6:off
winbind               0:off   1:off   2:off   3:off   4:off   5:off   6:off
xinetd                  0:off   1:off   2:on    3:on    4:on    5:on    6:off
about the service tomcat does not support chkconfig error , after some searching it turn’s out. In order to support chkconfig, an init script must:
1.  Be located in /etc/rc.d/init.d (which /etc/init.d is a symlink to)
2.  Have a commented out line that contains “chkconfig: <default  runlevels for this service> <start priority> <stop priority>”
3.  Have a commented out line that contains “description: <a description of the service>”
4.  Upon successful service startup, place a lock file in  /var/lock/subsys that matches the name of the service script.  Upon  successful service shutdown, the lockfile must be removed.

okay i ask my friend to add this on the tomcat script
#!/bin/sh
#
# Tomcat Server
#
# chkconfig: 345 96 30
# description: Java servlet container

and after some reboot itis working 🙂 , the tomcat script are executed on reboot time, here is the detail script

#!/bin/sh
#
# Tomcat Server
#
# chkconfig: 345 96 30
# description: Java servlet container

JAVA_HOME=/opt/jdk

PATH=${JAVA_HOME}/bin:${PATH}

TOMCAT_START=/opt/tomcat/bin/startup.sh

TOMCAT_STOP=/opt/tomcat/bin/shutdown.sh

export JAVA_HOME PATH

start()
{
if [ -x ${TOMCAT_START} ]; then
echo “Starting tomcat server…”
${TOMCAT_START} &
else
echo “Cannot start tomcat server”
fi
}

stop()
{
if [ -x ${TOMCAT_STOP} ]; then
echo “Stopping tomcat server…”
${TOMCAT_STOP} &
else
echo “Cannot stop tomcat server”
fi
}

restart()
{
stop
sleep 10
start
}

status()
{
echo “No status available for tomcat server”
}

case “$1” in
‘start’)
start
;;
‘stop’)
stop
;;
‘restart’)
restart
;;
‘status’)
status
;;
*)
echo “Please supply an argument [start|stop|restart]”
esac