linux Tomcat开机自启动

1 /etc/init.d目录下新建tomcat文件

注意chkconfig 和 description 是需要加上的,export 中的JAVA_HOME,CATALINA_HOME,CATALINA_BASE配置自己安装的java和tomcat路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcatd
# Tomcat auto-start
# Source function library.
. /etc/init.d/functions
# source networking configuration.
. /etc/sysconfig/network
RETVAL=0

export JAVA_HOME=/usr/local/software/java/jdk1.8.0_202
export CATALINA_HOME=/usr/local/software/tomcat/tomcat8
export CATALINA_BASE=/usr/local/software/tomcat/tomcat8

start()
{
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
RETVAL=$?
echo " OK"
return $RETVAL
fi
}
stop()
{
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
RETVAL=$?
sleep 1
ps -fwwu tomcat | grep apache-tomcat|grep -v grep | grep -v PID | awk '{print $2}'|xargs kill -9
echo " OK"
# [ $RETVAL -eq 0 ] && rm -f /var/lock/...
return $RETVAL
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
echo $"Restaring Tomcat"
$0 stop
sleep 1
$0 start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL

2 修改tomcat文件的执行权限

1
chmod 777 tomcat


3 验证tomcat文件

1
2
3
4
# 启动tomcat
service tomcat start
# 查看tomcat状态
systemctl status tomcat


4 chkconfig 添加tomcat服务

1
2
3
chkconfig --add tomcat 
chkconfig --list | grep tomcat
chkconfig tomcat on

如果得到的结果的3,4,5项为off状态,需要设置为开机自启动,chkconfig tomcat on,如果已经为开则无需修改


5 重启服务器进行验证

1
sudo reboot