Centos7把tomcat8做成服务
时间:2019-02-28 18:05 来源:linux.it.net.cn 作者:IT
tomcat做成服务,直接用systemctl命令来操作
第一步:修改tomcat的catalina.sh文件
我的tomcat解压路径是/usr/tomcat/apache-tomcat-8.5.38
vi /usr/tomcat/apache-tomcat-8.5.38/bin/catalina.sh
添加两行java和jre的环境。
-----------------------------------------------------没有实现的分割线---------------------------------------------------------------------
有的博客说修改CATALINA_PID,然后在/usr/lib/systemd/system创建tomcat.service文件,添加如下内容:
[Unit]
Description=Apache Tomcat 8
After=syslog.target network.target
[Service]
Type=forking
PIDFile=/opt/tomcat8/tomcat.pid
ExecStart=/opt/tomcat8/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
因为我的目录中没有tomcat.pid,我修改之后启动报如下错
[root@localhost bin]# systemctl start tomcat
Warning: tomcat.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Job for tomcat.service failed because the control process exited with error code. See "systemctl status tomcat.service" and "journalctl -xe" for details.
使用systemctl status tomcat.service查看的内容:
[root@localhost bin]# systemctl status tomcat.service
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 四 2019-02-21 20:14:35 CST; 10s ago
Process: 8355 ExecStart=/usr/tomcat/apache-tomcat-8.5.38/bin/startup.sh (code=exited, status=1/FAILURE)
2月 21 20:14:35 localhost.localdomain systemd[1]: Starting Tomcat...
2月 21 20:14:35 localhost.localdomain startup.sh[8355]: Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
2月 21 20:14:35 localhost.localdomain startup.sh[8355]: At least one of these environment variable is needed to run this program
2月 21 20:14:35 localhost.localdomain systemd[1]: tomcat.service: control process exited, code=exited status=1
2月 21 20:14:35 localhost.localdomain systemd[1]: Failed to start Tomcat.
2月 21 20:14:35 localhost.localdomain systemd[1]: Unit tomcat.service entered failed state.
2月 21 20:14:35 localhost.localdomain systemd[1]: tomcat.service failed.
提示既没有java_home 也没有jre_home环境,虽然知道没有java环境,仍然不知道加在哪里。
--------------------------------------------------------------------没有实现的分割线结束-----------------------------------------------------------------------
添加过java环境之后,再在/usr/lib/systemd/system目录下创建tomcat.service文件,添加如下内容
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=oneshot
ExecStart=/usr/tomcat/apache-tomcat-8.5.38/bin/startup.sh#自己解压的tomcat路径
ExecStop=/usr/tomcat/apache-tomcat-8.5.38/bin/shutdown.sh#自己解压的tomcat路径
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
:wq!保存推出,
重载配置文件systemctl daemon-reload启动tomcat
[root@localhost bin]# systemctl daemon-reload
[root@localhost bin]# systemctl start tomcat.service
[root@localhost bin]# systemctl stop tomcat.service
[root@localhost bin]# systemctl start tomcat.service
[root@localhost bin]# systemctl enable tomcat.service
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service.
(责任编辑:IT)
tomcat做成服务,直接用systemctl命令来操作
第一步:修改tomcat的catalina.sh文件
我的tomcat解压路径是/usr/tomcat/apache-tomcat-8.5.38
vi /usr/tomcat/apache-tomcat-8.5.38/bin/catalina.sh
添加两行java和jre的环境。
-----------------------------------------------------没有实现的分割线---------------------------------------------------------------------
有的博客说修改CATALINA_PID,然后在/usr/lib/systemd/system创建tomcat.service文件,添加如下内容:
[Unit]
Description=Apache Tomcat 8
After=syslog.target network.target
[Service]
Type=forking
PIDFile=/opt/tomcat8/tomcat.pid
ExecStart=/opt/tomcat8/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
因为我的目录中没有tomcat.pid,我修改之后启动报如下错
[root@localhost bin]# systemctl start tomcat
Warning: tomcat.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Job for tomcat.service failed because the control process exited with error code. See "systemctl status tomcat.service" and "journalctl -xe" for details.
使用systemctl status tomcat.service查看的内容:
[root@localhost bin]# systemctl status tomcat.service
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 四 2019-02-21 20:14:35 CST; 10s ago
Process: 8355 ExecStart=/usr/tomcat/apache-tomcat-8.5.38/bin/startup.sh (code=exited, status=1/FAILURE)
2月 21 20:14:35 localhost.localdomain systemd[1]: Starting Tomcat...
2月 21 20:14:35 localhost.localdomain startup.sh[8355]: Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
2月 21 20:14:35 localhost.localdomain startup.sh[8355]: At least one of these environment variable is needed to run this program
2月 21 20:14:35 localhost.localdomain systemd[1]: tomcat.service: control process exited, code=exited status=1
2月 21 20:14:35 localhost.localdomain systemd[1]: Failed to start Tomcat.
2月 21 20:14:35 localhost.localdomain systemd[1]: Unit tomcat.service entered failed state.
2月 21 20:14:35 localhost.localdomain systemd[1]: tomcat.service failed.
提示既没有java_home 也没有jre_home环境,虽然知道没有java环境,仍然不知道加在哪里。
--------------------------------------------------------------------没有实现的分割线结束-----------------------------------------------------------------------
添加过java环境之后,再在/usr/lib/systemd/system目录下创建tomcat.service文件,添加如下内容
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=oneshot
ExecStart=/usr/tomcat/apache-tomcat-8.5.38/bin/startup.sh#自己解压的tomcat路径
ExecStop=/usr/tomcat/apache-tomcat-8.5.38/bin/shutdown.sh#自己解压的tomcat路径
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
:wq!保存推出,
重载配置文件systemctl daemon-reload启动tomcat
[root@localhost bin]# systemctl daemon-reload
[root@localhost bin]# systemctl start tomcat.service
[root@localhost bin]# systemctl stop tomcat.service
[root@localhost bin]# systemctl start tomcat.service
[root@localhost bin]# systemctl enable tomcat.service
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service.
(责任编辑:IT) |