systemctl 在启动后停止 tomcat

systemctl stops tomcat after start

我正在尝试使用 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)它直接崩溃了。

systemctl状态的输出tomcat8:

● 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 命令已执行。

另一台服务器运行那个配置没问题,但我到现在还没机会得到它运行。

有人知道这种情况下的问题是什么吗?

谢谢!

只有最新的 8.5.37 版本才会出现这个问题。试试 8.5.35 就可以了。

出于某种原因,在 8.5.37 中发现的 catalina.sh 的新版本似乎有一些 Systemd 不喜欢的变量管理。

我在 ubuntu 18.04 上的 8.5.37 遇到了同样的问题。

我比较了 8.5.35 和 8.5.37 之间的 catalina.sh,有问题的更改似乎在第 482 和 489 行(如果您使用 -安全选项将位于第 470 和 479 行),我将此行更改为 8.5.35 中的行,它开始时没有问题。

此更改与添加“$CATALINA_OUT_CMD”选项有关。

这里的正确答案是 "update Tomcat",因为这是由启动脚本中的错误引起的,如

中所述