tomcat8 开机自启动
参考:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Services.html#sect-Managing_Services_with_systemd-Services-List
http://linux.it.net.cn/CentOS/fast/2015/0202/12829.html
在tomcat的bin目录下创建setenv.sh,catalina.sh 启动时候会自动调用
[root@klmy-460-res01 ~]# vi /usr/local/apache-tomcat-8.5.6/bin/setenv.sh
编辑setenv.sh
#add tomcat pid
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#add java opts
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"
添加tomcat.service
[root@klmy-460-res01 ~]# vi /usr/lib/systemd/system/tomcat.service
添加内容:
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/apache-tomcat-8.5.6/tomcat.pid
ExecStart=/usr/local/apache-tomcat-8.5.6/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
配置开机启动tomcat.service
[root@klmy-460-res01 ~]# systemctl enable tomcat
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service.
启动tomcat服务
[root@klmy-460-res01 ~]# systemctl start tomcat
Job for tomcat.service failed because the control process exited with error code. See "systemctl status tomcat.service" and "journalctl -xe" for details.
启动失败,有错误查看错误信息:
[root@klmy-460-res01 ~]# systemctl status tomcat
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 五 2016-12-16 17:52:49 CST; 5s ago
Process: 34944 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=1/FAILURE)
Process: 34953 ExecStart=/usr/local/apache-tomcat-8.5.6/bin/startup.sh (code=exited, status=1/FAILURE)
Main PID: 34899 (code=exited, status=0/SUCCESS)
12月 16 17:52:49 klmy-460-res01 systemd[1]: Starting Tomcat...
12月 16 17:52:49 klmy-460-res01 startup.sh[34953]: Neither the JAVA_HOME nor the JRE_HOME environment varia...ined
12月 16 17:52:49 klmy-460-res01 startup.sh[34953]: At least one of these environment variable is needed to ...gram
12月 16 17:52:49 klmy-460-res01 systemd[1]: tomcat.service: control process exited, code=exited status=1
12月 16 17:52:49 klmy-460-res01 systemd[1]: Failed to start Tomcat.
12月 16 17:52:49 klmy-460-res01 systemd[1]: Unit tomcat.service entered failed state.
12月 16 17:52:49 klmy-460-res01 systemd[1]: tomcat.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
经过检查$JAVA_HOME 和$JRE_HOME都是正常的。
解决办法在catlina.sh 最上面导入home:
export JAVA_HOME=/usr/local/jdk1.8.0_111
export JRE_HOME=/usr/local/jdk1.8.0_111/jre
重新启动服务并查看状态:
[root@klmy-460-res01 ~]# systemctl start tomcat
[root@klmy-460-res01 ~]# systemctl status tomcat
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2016-12-16 18:02:14 CST; 3s ago
Process: 35021 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=1/FAILURE)
Process: 35055 ExecStart=/usr/local/apache-tomcat-8.5.6/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 35063 (java)
CGroup: /system.slice/tomcat.service
└─35063 /usr/local/jdk1.8.0_111/jre/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-8.5.6/conf/logging.properties -Djava.util.logging.manager=org...
12月 16 18:02:14 klmy-460-res01 systemd[1]: Starting Tomcat...
12月 16 18:02:14 klmy-460-res01 startup.sh[35055]: Using CATALINA_BASE: /usr/local/apache-tomcat-8.5.6
12月 16 18:02:14 klmy-460-res01 startup.sh[35055]: Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.6
12月 16 18:02:14 klmy-460-res01 startup.sh[35055]: Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.6/temp
12月 16 18:02:14 klmy-460-res01 startup.sh[35055]: Using JRE_HOME: /usr/local/jdk1.8.0_111/jre
12月 16 18:02:14 klmy-460-res01 startup.sh[35055]: Using CLASSPATH: /usr/local/apache-tomcat-8.5.6/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.6...-juli.jar
12月 16 18:02:14 klmy-460-res01 startup.sh[35055]: Using CATALINA_PID: /usr/local/apache-tomcat-8.5.6/tomcat.pid
12月 16 18:02:14 klmy-460-res01 startup.sh[35055]: Tomcat started.
12月 16 18:02:14 klmy-460-res01 systemd[1]: Started Tomcat.
Hint: Some lines were ellipsized, use -l to show in full.
重启服务器:
[root@klmy-460-res01 ~]# firewall-cmd --zone=public --list-ports
443/tcp 80/tcp 8080/tcp
[root@klmy-460-res01 ~]# reboot
客户端windows 命令行测试一下:
telnet 服务器ip 端口