1 进入到 /etc/init.d文件夹下 需要使用到的命令: cd /etc/init.d/
2 把下面的代码保存为tomcat文件,并让它成为可执行文件 chmod 755 tomcat.
#!/bin/bash
# This is the init script for starting up the
# Jakarta Tomcat server
#
# chkconfig: 345 91 10
# description: Starts and stops the Tomcat daemon.
#
# Source function library.
. /etc/rc.d/init.d/functions
# Get config.
. /etc/sysconfig/network
# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0
export JAVA_HOME=/www/javaWeb/java64/jdk1.7.0_80 // 自己的java文件地址
tomcat_home=/www/javaWeb/apache-tomcat-8.5-cmd // 自己的tomcat路径
startup=$tomcat_home/bin/startup.sh
shutdown=$tomcat_home/bin/shutdown.sh
start(){
echo -n "Starting Tomcat service:"
cd $tomcat_home
$startup
echo "tomcat is succeessfully started up"
}
stop(){
echo -n "Shutting down tomcat: "
cd $tomcat_home
$shutdown
echo "tomcat is succeessfully shut down."
}
status(){
numproc=`ps -ef | grep catalina | grep -v "grep catalina" | wc -l`
if [ $numproc -gt 0 ]; then
echo "Tomcat is running..."
else
echo "Tomcat is stopped..."
fi
}
restart(){
stop
start
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
2 将文件加入到服务队列中
chkconfig --add tomcat
3 查看tomcat 文件是否加入服务列表成功
<span style="color:#330033">chkconfig --list </span>
4 设置服务开机自启动
chkconfig tomcat on
5 删除服务
chkconfig --del tomcat