我已经在 Ubuntu 18.04 上安装了 Tomcat 9,并尝试将其添加到服务中。
但是,与其他用户不同,tomcat.pid
文件在任何地方都不存在。所以我的 Tomcat 服务文件生成错误。
[Unit]
Description=Apache Tomcat 9 Servlet Container
After=syslog.target network.target
[Service]
User=nblizz
Group=nblizz
Type=forking
Environment=CATALINA_PID=/opt/apache-tomcat-9.0.16/tomcat.pid <--- !! (Not exist)
Environment=CATALINA_HOME=/opt/apache-tomcat-9.0.16
Environment=CATALINA_BASE=/opt/apache-tomcat-9.0.16
ExecStart=/opt/apache-tomcat-9.0.16/bin/startup.sh
ExecStop=/opt/apache-tomcat-9.0.16/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
运行Tomcat没有问题,但是停止Tomcat时出现错误信息。
● apache-tomcat.service - Apache Tomcat 9 Servlet Container
Loaded: loaded (/etc/systemd/system/apache-tomcat.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2019-03-16 01:30:39 KST; 1s ago
Process: 1462 ExecStop=/opt/apache-tomcat-9.0.16/bin/shutdown.sh (code=exited, status=1/FAILURE) <--- !!
Process: 545 ExecStart=/opt/apache-tomcat-9.0.16/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 573 (code=killed, signal=TERM)
Mar 16 01:17:29 ndelt-personal systemd[1]: Starting Apache Tomcat 9 Servlet Container...
Mar 16 01:17:29 ndelt-personal startup.sh[545]: Tomcat started.
Mar 16 01:17:29 ndelt-personal systemd[1]: Started Apache Tomcat 9 Servlet Container.
Mar 16 01:30:39 ndelt-personal systemd[1]: Stopping Apache Tomcat 9 Servlet Container...
Mar 16 01:30:39 ndelt-personal shutdown.sh[1462]: $CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted.
Mar 16 01:30:39 ndelt-personal systemd[1]: apache-tomcat.service: Control process exited, code=exited status=1
Mar 16 01:30:39 ndelt-personal systemd[1]: apache-tomcat.service: Failed with result 'exit-code'.
Mar 16 01:30:39 ndelt-personal systemd[1]: Stopped Apache Tomcat 9 Servlet Container.
另外,文件的颜色apache-tomcat.service
与其他文件不同。(https://i.stack.imgur.com/WnD3Y.png)
问题是什么?
答案1
看起来 catalina.sh 文件与 v9.0.14 相比有所变化
$ diff bin/catalina.sh ../tomcats/apache-tomcat-9.0.14/bin/catalina.sh
479c479
< 2\>\&1 \&\& echo \$! \>\“$catalina_pid_file\” \; \} $catalina_out_command “&”
> 2\>\&1 \& echo \$! \>\“$catalina_pid_file\” \; \} $catalina_out_command “&”
489c489
< 2\>\&1 \&\& echo \$! \>\“$catalina_pid_file\” \; \} $catalina_out_command “&”
> 2\>\&1 \& echo \$! \>\“$catalina_pid_file\” \; \} $catalina_out_command “&”
如果删除那里多余的“&”符号,则 pid 文件将按预期创建。