Set Tomcat to automatically start at boot on utuntu

one. Environment:
Operating system: utuntu11.10
JAVA_HOME: /document/opt/jdk1.6
Tomcat installation directory: /documet/opt/tomcat6
II. Write a service startup script Create a file tomcatd
under /etc/init.d/:
#! /bin/sh
# chkconfig: 35 30 70
# descripttion: VirtualBox Linux kernel module
#
### BEGIN INIT INFO
# Provides: tomcat
# Required- Start: $remote_fs $network
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Descripttion: tomcat
### END INIT INFO
export JAVA_HOME=/document/opt/ jdk1.6
export CATALINA_HOME=/document/opt/tomcat-6
export CATALINA_PID=/var/run/tomcatd.pid
PIDFILE=/var/run/tomcatd.pid
pidof_tomcat() {
# if there is actually an apache2 process whose pid is in PIDFILE,
# print it and return 0.
if [ -e "$PIDFILE" ]; then
if pidof java | tr ' ' '\n' | grep -w $(cat $PIDFILE); then
return 0
fi
fi
return 1
}
start() {
PID=$(pidof_tomcat) || true
if [ -n "$PID" ]; then
echo "tomcat is running (pid $PID)."
exit 0
else
sh $CATALINA_HOME/bin/startup.sh
fi
}
stop() {
PID=$(pidof_tomcat) || true
if [ -n "$PID" ]; then
sh $CATALINA_HOME/bin/shutdown.sh
PID=$(pidof_tomcat) || true
if [ -n "$PID" ]; then
echo "tomcat is still running (pid $PID)."
echo "kill process..."
kill -9 $PID
fi
else
echo "tomcat is NOT running."
fi
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
PID=$(pidof_tomcat) || true
if [ -n "$PID" ]; then
echo "tomcat is running (pid $PID)."
exit 0
else
echo "tomcat is NOT running."
if [ -e "$PIDFILE" ]; then
exit 1
else
exit 3
fi
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
3. Add service:
[root@root init.d]# chmod uog+rx tomcatd
[root@root init.d]# sysv-rc-conf --level 2345 tomcatd on
four. Note:
1. In the comment statement of the first two lines of the tomcat file, you need to include two parts of chkconfig and descriptionttion (make sure not to misspelling), otherwise when "chkconfig --add tomcatd" is executed, "tomcatd service does not support" will appear chkconfig" error message.
2. If prompted that the sysv-rc-conf command does not exist, execute apt-get install sysv-rc-conf to install it.
3. If the tomcat user does not exist, create it through the following command: #useradd -U -s /sbin/nologin tomcat
View the service addition result:
[root@root init.d]# sysv-rc-conf --list tomcatd

tomcatd 2:on 3:on 4:on 5:on
[root@root init.d]# /etc/init.d/tomcatd start
Delete service:
[root@root init.d]# sysv-rc-conf tomcatd off

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705173&siteId=291194637