当前位置: 首页 > 知识库问答 >
问题:

CentOS 7上的Tomcat 8不会作为服务启动(但它手动启动......)

马祺
2023-03-14

我已经在CentOS虚拟机上安装了Tomcat(apache-Tomcat-8.5.37)(使用bento/CentOS-7)。

Tomcat安装在

/opt/tomcat/apache-tomcat-8.5.37

Java8安装在

/usr/java/jre1.8.0_192-amd64

在安装Tomcat之前,我已经用这种方式创建了一个Tomcat用户

sudo useradd tomcat
sudo passwd tomcat
sudo mkdir /opt/tomcat
sudo chown -R tomcat:tomcat /opt/tomcat
su – tomcat

如果我尝试手动启动Tomcat,请使用

/opt/tomcat/apache-tomcat-8.5.37/bin/startup.sh

一切都很好,我可以从url测试Tomcat广告的示例

http://192.168.33.10:8080

(注意:192.168.33.10是我的虚拟机的IP…)

我还可以使用

/opt/tomcat/apache-tomcat-8.5.37/bin/shutdown.sh

我想启动类似Tomcat的e服务,所以我停止了Tomcat,然后编辑了一个文件

/etc/systemd/system/tomcat.service

有了这个内容

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/java/jre1.8.0_192-amd64
Environment=CATALINA_PID=/opt/tomcat/apache-tomcat-8.5.37/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/apache-tomcat-8.5.37/
Environment=CATALINA_BASE=/opt/tomcat/apache-tomcat-8.5.37/
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

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

User=tomcat
Group=tomcat
UMask=0007

[Install]
WantedBy=multi-user.target

然后呢

sudo systemctl daemon-reload

sudo systemctl start tomcat

Tomcat现在调用url没有响应

http://192.168.33.10:8080

和的输出

sudo systemctl status tomcat

是以下内容

● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2019-01-01 09:34:46 UTC; 13min ago
  Process: 14438 ExecStop=/bin/kill -15 $MAINPID (code=exited, status=1/FAILURE)
  Process: 14425 ExecStart=/opt/tomcat/apache-tomcat-8.5.37/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 14436 (code=exited, status=0/SUCCESS)

Jan 01 09:34:46 localhost.localdomain systemd[1]: Starting Apache Tomcat Web Application Container...
Jan 01 09:34:46 localhost.localdomain startup.sh[14425]: Existing PID file found during start.
Jan 01 09:34:46 localhost.localdomain startup.sh[14425]: Removing/clearing stale PID file.
Jan 01 09:34:46 localhost.localdomain systemd[1]: Started Apache Tomcat Web Application Container.
Jan 01 09:34:46 localhost.localdomain systemd[1]: tomcat.service: control process exited, code=exited status=1
Jan 01 09:34:46 localhost.localdomain systemd[1]: Unit tomcat.service entered failed state.
Jan 01 09:34:46 localhost.localdomain systemd[1]: tomcat.service failed.

的输出

journalctl -xe

是以下内容

Jan 01 09:34:46 localhost.localdomain sudo[14418]:   tomcat : TTY=pts/0 ; PWD=/opt/tomcat/apache-tomcat-8.5.37/temp ; USER=root ; COMMAND=/bin/systemctl start tomcat
Jan 01 09:34:46 localhost.localdomain polkitd[635]: Registered Authentication Agent for unix-process:14419:1135902 (system bus name :1.85 [/usr/bin/pkttyagent --notify-fd 5 --fallb
Jan 01 09:34:46 localhost.localdomain systemd[1]: Starting Apache Tomcat Web Application Container...
-- Subject: Unit tomcat.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit tomcat.service has begun starting up.
Jan 01 09:34:46 localhost.localdomain startup.sh[14425]: Existing PID file found during start.
Jan 01 09:34:46 localhost.localdomain startup.sh[14425]: Removing/clearing stale PID file.
Jan 01 09:34:46 localhost.localdomain startup.sh[14425]: Tomcat started.
Jan 01 09:34:46 localhost.localdomain polkitd[635]: Unregistered Authentication Agent for unix-process:14419:1135902 (system bus name :1.85, object path /org/freedesktop/PolicyKit1
Jan 01 09:34:46 localhost.localdomain systemd[1]: Started Apache Tomcat Web Application Container.
-- Subject: Unit tomcat.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit tomcat.service has finished starting up.
--
-- The start-up result is done.
Jan 01 09:34:46 localhost.localdomain kill[14438]: Usage:
Jan 01 09:34:46 localhost.localdomain kill[14438]: kill [options] <pid|name> [...]
Jan 01 09:34:46 localhost.localdomain kill[14438]: Options:
Jan 01 09:34:46 localhost.localdomain kill[14438]: -a, --all              do not restrict the name-to-pid conversion to processes
Jan 01 09:34:46 localhost.localdomain kill[14438]: with the same uid as the present process
Jan 01 09:34:46 localhost.localdomain kill[14438]: -s, --signal <sig>     send specified signal
Jan 01 09:34:46 localhost.localdomain kill[14438]: -q, --queue <sig>      use sigqueue(2) rather than kill(2)
Jan 01 09:34:46 localhost.localdomain kill[14438]: -p, --pid              print pids without signaling them
Jan 01 09:34:46 localhost.localdomain kill[14438]: -l, --list [=<signal>] list signal names, or convert one to a name
Jan 01 09:34:46 localhost.localdomain kill[14438]: -L, --table            list signal names and numbers
Jan 01 09:34:46 localhost.localdomain kill[14438]: -h, --help     display this help and exit
Jan 01 09:34:46 localhost.localdomain kill[14438]: -V, --version  output version information and exit
Jan 01 09:34:46 localhost.localdomain kill[14438]: For more details see kill(1).
Jan 01 09:34:46 localhost.localdomain systemd[1]: tomcat.service: control process exited, code=exited status=1
Jan 01 09:34:46 localhost.localdomain systemd[1]: Unit tomcat.service entered failed state.
Jan 01 09:34:46 localhost.localdomain systemd[1]: tomcat.service failed.
Jan 01 09:34:59 localhost.localdomain sudo[14451]:   tomcat : TTY=pts/0 ; PWD=/opt/tomcat/apache-tomcat-8.5.37/temp ; USER=root ; COMMAND=/bin/systemctl status tomcat
Jan 01 09:48:45 localhost.localdomain sudo[15115]:   tomcat : TTY=pts/0 ; PWD=/opt/tomcat/apache-tomcat-8.5.37/bin ; USER=root ; COMMAND=/bin/systemctl status tomcat

卡特琳娜的产量。下面是

01-Jan-2019 09:09:21.585 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
01-Jan-2019 09:09:21.602 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
01-Jan-2019 09:09:21.614 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 2829 ms
01-Jan-2019 09:09:21.697 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
01-Jan-2019 09:09:21.697 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.37
01-Jan-2019 09:09:21.738 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/ROOT]
01-Jan-2019 09:09:22.777 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/ROOT] has finished in [1,038] ms
01-Jan-2019 09:09:22.795 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/docs]
01-Jan-2019 09:09:22.847 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/docs] has finished in [70] ms
01-Jan-2019 09:09:22.847 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/examples]
01-Jan-2019 09:09:23.468 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/examples] has finished in [621] ms
01-Jan-2019 09:09:23.469 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/host-manager]
01-Jan-2019 09:09:23.522 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/host-manager] has finished in [54] ms
01-Jan-2019 09:09:23.522 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/manager]
01-Jan-2019 09:09:23.569 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/apache-tomcat-8.5.37/webapps/manager] has finished in [47] ms
01-Jan-2019 09:09:23.592 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
01-Jan-2019 09:09:23.601 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
01-Jan-2019 09:09:23.604 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1990 ms
01-Jan-2019 09:12:38.830 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
01-Jan-2019 09:12:38.831 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]
01-Jan-2019 09:12:38.870 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
01-Jan-2019 09:12:38.878 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina]
01-Jan-2019 09:12:39.117 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
01-Jan-2019 09:12:39.123 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
01-Jan-2019 09:12:39.131 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
01-Jan-2019 09:12:39.137 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]

????

感谢您的建议!!

共有3个答案

夹谷沛
2023-03-14

一个常见的问题是,当您手动启动tomcat一次(使用您自己的用户帐户)时,临时文件、日志等由您所有,而不是由守护进程用户(tomcat,根据引用的tomcat.service文件)所有。由于缺少权限,后续启动守护进程用户无法覆盖这些文件-这也可能会阻止正确记录这些问题。

修复这些权限(或者更确切地说:文件所有权),然后重试。我的守护程序启动脚本通常对相关文件有一个chown,只是为了确保我不会遇到这些问题,以防有人手动开始使用错误的用户帐户。

陆文斌
2023-03-14

我发现了相关的bug报告:

错误63063

错误63041

此错误将在下一版本中修复。

狄楷
2023-03-14

注:截至2019年10月

  1. 我按照BUGhttps://bz.apache.org/bugzilla/show_bug.cgi?id=63063,仍然没有解决问题。
  2. 我几乎没有试错,问题得到了解决。它工作流畅,按照标准运行完全正常。即使您优雅地杀死tomcat服务或使用kill-9然后使用systemctl启动tomcat服务,如下所述,它的工作方式很有魅力。

我使用的是Ubuntu 16.04、tomcat 8.5.37、Java 1.8.0_222

我使用了下面的systemd脚本,将其保存在位置:/etc/systemd/system/

# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container for ShopWorx
After=syslog.target network.target

[Service]
Type=forking
WorkingDirectory=/usr/local/tomcat/bin

ExecStart=/bin/bash /usr/local/tomcat/bin/catalina.sh start
ExecStop=/bin/bash /usr/local/tomcat/bin/catalina.sh stop

User=emgda
Group=emgda

[Install]
WantedBy=multi-user.target

并按顺序在命令下运行

systemctl daemon-reload

systemctl start tomcat

systemctl status tomcat

下面是tomcat服务状态的输出。

● tomcat.service - Apache Tomcat Web Application Container for ShopWorx
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-10-09 14:52:08 IST; 1min 46s ago
  Process: 3713 ExecStart=/bin/bash /usr/local/tomcat/bin/catalina.sh start (code=exited, status=0/SUCCESS)
 Main PID: 3720 (bash)
   CGroup: /system.slice/tomcat.service
           ├─3720 /bin/bash /usr/local/tomcat/bin/catalina.sh start
           └─3722 /usr/lib/jvm/java-8-openjdk-amd64/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.

Oct 09 14:52:08 ubuntu systemd[1]: Starting Apache Tomcat Web Application Container ...
Oct 09 14:52:08 ubuntu bash[3713]: Tomcat started.
Oct 09 14:52:08 ubuntu systemd[1]: Started Apache Tomcat Web Application Container.

重要提示:必须注意WorkingDirectoryExecStartExecStop,它们的值至关重要。

 类似资料:
  • 问题内容: 我在运行Ubuntu 16.04 LTS的ARM Odroid XU4上安装了ELasticsearch 6和JDK 1.8.0_161。没有错误。 当我尝试将Elasticsearch作为服务启动时,无法在端口9200上连接到localhost,并且服务状态显示为: 没有日志。不知道在哪里看。nofile限制在中设置为65536 。我不知道在其他地方可以找到诊断信息。 虽然,当我通过

  • 我正在使用Vagrant部署到Ubuntu Linux,并尝试启动服务。 然后我跟踪,发现: ?tomcat8.service-lsb:启动tomcat。加载:加载(/etc/init.d/tomcat8) active:失败(结果:exit-code)自2016-03-28 09:44:17 GMT;5s前 文档:人:systemd-sysv-generator(8) 进程:884 execst

  • 问题内容: 如果运行以下命令,Redis会正确启动: 我添加了我的启动脚本: 但是,当我从全新启动中启动时,Redis无法运行。我应该在哪里解决此问题? 问题答案: 这个东西通常设置为20。现在,redis需要联网还为时过早。 因此,请尝试以下操作:

  • 我的XAMPP服务器有问题。我可以毫无问题地启动Apache、FileZilla和其他所有东西,但当我尝试启动MySQL时,它只是说 尝试启动MySQL服务... 它永远都是这样,就像被冻了一样。 有什么好办法吗?

  • 我希望payara服务器作为服务运行。我以sudo的身份登录asadmin,并使用create-service命令。给出了以下输出。 这将在 /etc/init.d/ 文件夹中创建payara_production脚本,但一旦重新启动计算机,就不会执行此脚本。我必须手动启动payara才能运行它。 “您已经创建了服务,但是您需要自己启动它”是什么意思,我在之前使用的GlassFish版本中没有类似

  • 我很久以前做了一些自定义systemd服务,都有相同的配置(当然ExecStart除外) 这种配置已经运行了很多年,从18.04版LTS开始,我已经启动并运行了ubuntu,但是现在看起来有些systemd服务根本没有启动,配置如下(myapp.service): 服务已启用: 如果在重新启动后执行“systemctl status myapp”: 如果我在重新启动后执行“Journal alct