我正在尝试使用systemctl配置Tomcat 8.5.37,以在Ubuntu 16.04上将服务器作为守护程序启动。
为此,我在/etc/systemd/system/tomcat8.service中创建了以下服务文件:
[Unit]
Description=Tomcat Server
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/opt/jdk1.8
Environment=CATALINA_PID=/opt/tomcat8/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat8
Environment=CATALINA_BASE=/opt/tomcat8
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -Dlog4j.configurationFile=/opt/conf/log4j2.xml'
Environment='JAVA_OPTS=-Djava.net.preferIPv4Stack=true -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat8/bin/startup.sh
ExecStop=/opt/tomcat8/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
#RestartSec=40
#Restart=always
[Install]
WantedBy=multi-user.target
Tomcat服务器是直接从apache下载的,仅被提取。 JDK是最新的Oracle JDK 1.8。
当我直接通过startup.sh启动tomcat时,一切都很好。 但是,当我使用systemctl(之前已完成daemon-reload)时,它直接崩溃了。
tomcat8的systemctl状态输出:
● tomcat8.service - Tomcat Server
Loaded: loaded (/etc/systemd/system/tomcat8.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2019-01-04 23:14:08 CET; 2s ago
Process: 9793 ExecStop=/opt/tomcat8/bin/shutdown.sh (code=exited, status=0/SUCCESS)
Process: 9781 ExecStart=/opt/tomcat8/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 9791 (code=exited, status=0/SUCCESS)
journalctl -xe的输出:
Jan 04 23:14:04 myserver.com systemd[1]: Starting Tomcat Server...
-- Subject: Unit tomcat8.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit tomcat8.service has begun starting up.
Jan 04 23:14:04 myserver.com startup.sh[9781]: Tomcat started.
Jan 04 23:14:04 myserver.com systemd[1]: Started Tomcat Server.
-- Subject: Unit tomcat8.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit tomcat8.service has finished starting up.
--
-- The start-up result is done.
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: Jan 04, 2019 11:14:06 PM org.apache.catalina.startup.Catalina stopServer
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: SEVERE: Could not contact [localhost:[8005]]. Tomcat may not be running.
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: Jan 04, 2019 11:14:07 PM org.apache.catalina.startup.Catalina stopServer
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: SEVERE: Catalina.stop:
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: java.net.ConnectException: Connection refused (Connection refused)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.PlainSocketImpl.socketConnect(Native Method)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.Socket.connect(Socket.java:589)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.Socket.connect(Socket.java:538)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.Socket.<init>(Socket.java:434)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.net.Socket.<init>(Socket.java:211)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:503)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at java.lang.reflect.Method.invoke(Method.java:498)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:406)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:498)
Jan 04 23:14:07 myserver.com shutdown.sh[9793]: The stop command failed. Attempting to signal the process to stop through OS signal.
例外不是问题,因为它尝试关闭不再存在的服务。
我可以看到的是,将创建日志文件,这使我明白了启动过程已经开始。 但是我对状态输出中的过程感到困惑。有一个启动过程和一个停止过程。
貌似startup.sh开始运行,但是退出代码错误或无法识别,并且systemd在执行startup.sh之后立即运行ExecStop命令。
在该配置下,另一台服务器可以正常运行,但是到目前为止,我没有机会让它运行。
有人知道这种情况下是什么问题吗?
谢谢!
答案 0 :(得分:0)
这是仅在最新的8.5.37版本中发生的问题。尝试使用8.5.35,它将起作用。
出于某种原因,似乎在8.5.37中发现的catalina.sh新版本具有一些Systemd不喜欢的变量管理。
答案 1 :(得分:0)
我在ubuntu 18.04上遇到8.5.37的相同问题。
我在8.5.35和8.5.37之间比较了 catalina.sh ,有问题的更改似乎在第482和489行(如果使用-security选项,它将在网上470和479),我用8.5.35中的内容更改了这一行,开始时没有问题。
此更改与“ $ CATALINA_OUT_CMD”选项的添加有关。
答案 2 :(得分:0)
正确的答案是“更新Tomcat”,因为这是由启动脚本中的错误引起的,如https://stackoverflow.com/a/54452870/4735682
中所述