Tomcatstartup.sh 文件可以工作,但无法启动 tomcat.service

Tomcatstartup.sh 文件可以工作,但无法启动 tomcat.service

短篇故事:

当运行“sudo systemctl start tomcat”时,似乎在startup.sh之后立即调用shutdown.sh:

Feb 03 19:36:49 xxxxx startup.sh[10285]: Tomcat started.
Feb 03 19:36:49 xxxxx shutdown.sh[10294]: NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: Feb 03, 2019 7:36:53 PM org.apache.catalina.startup.Catalina stopServer

而如果我手动调用startup.sh那就没问题了。

很长的故事:

我已经使用以下脚本设置了下载并安装的 Tomcat:

#Add Tomcat group
sudo groupadd tomcat 
#Add Tomcat user, creating a home directory.
sudo useradd -m -g tomcat -d /opt/tomcat -s /bin/nologin tomcat
#Move to temp directory
cd /tmp
#Get Tomcat tar file
wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.14/bin/apache-                
tomcat-9.0.14.tar.gz
#Extract tar file to /opt/tomcat folder
tar -zxvf apache-tomcat-9.0.14.tar.gz -C /opt/tomcat/ --strip-components=1
#Give tomcat group recursive ownership of the installation directory
sudo chgrp -R tomcat /opt/tomcat
cd /opt/tomcat
#Give tomcat user execute access and reursive read access to conf
sudo chmod -R g+r conf
sudo chmod g+x conf
#Make tomcat user ownership of required directories
sudo chown -R tomcat webapps/ work/ temp/ logs/
#Make scripts in bin executable
sudo chmod +x /opt/tomcat/bin/*.sh

我将此文件添加到/etc/systemd/system/tomcat.service:

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/local/jdk-11.0.1
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

然后如果我像这样运行它:

systemctl daemon-reload
systemctl enable tomcat
systemctl start tomcat

它无法启动。

我从“systemctl status tomcat -l”得到以下输出:

tomcat.service - Apache Tomcat Web Application Container
  Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
  Active: activating (auto-restart) (Result: exit-code) since Sun 2019-02-03 12:49:15 UTC; 3s ago
Process: 9767 ExecStop=/bin/kill -15 $MAINPID (code=exited, status=1/FAILURE)
Process: 9754 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 9765 (code=exited, status=0/SUCCESS)

Feb 03 12:49:15 xxxxx systemd[1]: Unit tomcat.service entered failed state.
Feb 03 12:49:15 xxxxx systemd[1]: tomcat.service failed.

和 'journalctl -u tomcat.service -l' 返回:

Feb 03 12:48:12 xxxxx systemd[1]: Starting Apache Tomcat Web Application Container...
Feb 03 12:48:12 xxxxx startup.sh[9661]: Existing PID file found during start.
Feb 03 12:48:13 xxxxx startup.sh[9661]: Removing/clearing stale PID file.
Feb 03 12:48:13 xxxxx systemd[1]: Started Apache Tomcat Web Application Container.
Feb 03 12:48:13 xxxxx systemd[1]: tomcat.service: control process exited, code=exited status=1
Feb 03 12:48:13 xxxxx systemd[1]: Unit tomcat.service entered failed state.
Feb 03 12:48:13 xxxxx systemd[1]: tomcat.service failed.

如果我添加文件“/opt/tomcat/bin/setenv.sh”,内容为:

export JAVA_HOME=/usr/local/jdk-11.0.1

然后如果我运行:

sudo /opt/tomcat/bin/startup.sh

然后Tomcat就可以正常启动了。这是否表明我的 tomcat.service 文件有问题?

操作系统是Centos 7。

编辑1:

另一件值得一提的事情是:以 tomcat 用户身份运行startup.sh命令:

sudo -u tomcat /opt/tomcat/bin/startup.sh

运行一切正常,所以我不认为这是用户权限的问题。

编辑2:

我认为 PID 错误具有误导性。我将“/etc/systemd/system/tomcat.service”的 ExecStop 行更改为:

ExecStop=/opt/tomcat/bin/shutdown.sh

现在“journalctl -u tomcat.service -l”的输出如下所示:

Feb 03 19:36:48 xxxxx systemd[1]: Started Apache Tomcat Web Application Container.
Feb 03 19:36:48 xxxxx systemd[1]: Starting Apache Tomcat Web Application Container...
Feb 03 19:36:49 xxxxx startup.sh[10285]: Tomcat started.
Feb 03 19:36:49 xxxxx shutdown.sh[10294]: NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: Feb 03, 2019 7:36:53 PM org.apache.catalina.startup.Catalina stopServer
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: SEVERE: Could not contact [localhost:8005] (base port [8005] and offset [0]). Tomcat may not be running.
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: Feb 03, 2019 7:36:54 PM org.apache.catalina.startup.Catalina stopServer
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: SEVERE: Error stopping Catalina
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: java.net.ConnectException: Connection refused (Connection refused)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.Socket.connect(Socket.java:591)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.Socket.connect(Socket.java:540)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.Socket.<init>(Socket.java:436)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.net.Socket.<init>(Socket.java:213)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:513)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at java.base/java.lang.reflect.Method.invoke(Method.java:566)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:403)
Feb 03 19:36:54 xxxxx shutdown.sh[10294]: at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:497)

答案1

这个问题终于帮我解答了:

https://stackoverflow.com/questions/34614710/why-is-systemd-stopping-service-immediately-after-it-is-started

在 /etc/systemd/system/tomcat.service 中我删除了

Type=forking

Restart=always

Type=oneshot

RemainAfterExit=yes

它起作用了。因为显然你必须告诉服务不要立即关闭,而我阅读的 600 篇关于此事的教程中没有一篇提到这一点。

相关内容