1、进入init.d目录
cd /etc/rc.d/init.d
2、创建tomcat脚本
vim tomcat
3、编写启动脚本
#!/bin/bash
# chkconfig: 2345 10 90
# description: Start and Stop tomcat by service
export JAVA_HOME=/usr/local/java/jdk1.8.0_151
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.5.24
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/bin/commons-daemon.jar:$CATALINA_HOME/bin/bootstrap.jar
# source function library.
. /etc/rc.d/init.d/functions
if [ ! -f $CATALINA_HOME/bin/catalina.sh ]
then echo "Tomcat not valilable..."
exit
fi
# start
start(){
echo -n "Starting Tomcat: "
$CATALINA_HOME/bin/startup.sh
echo
}
#stop
stop(){
echo -n "Shutting down Tomcat: "
$CATALINA_HOME/bin/shutdown.sh
}
#restart
restart(){
stop
sleep 3
start
}
#status
status(){
ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}' | wc | awk '{print $2}' > /tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo -n "tomcat ( pid "
ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'
echo -n ") is running..."
echo
else
echo "Tomcat is stopped"
fi
}
case "$1" in
start)
start ;;
stop)
stop ;;
restart)
stop
sleep 3
start ;;
status)
status ;;
*)
echo "Usage: tomcatd {start|stop|restart|status}"
exit 1
esac
exit 0
4、
修改文件权限
chmod a+x tomcat
5、注册
tomcat服务
chkconfig --add tomcat
6、查看
tomcat服务是否被添加
chkconfig –list |grep tomcat
7、运行
service tomcat start
service tomcat stop
service tomcat restart
service tomcat status