正如here和here所述,我正在尝试(在Ubuntu 16.04上)在端口80上启动Tomcat 9.0.2,让systemd
预先初始化套接字特权端口,然后将其传递给作为非特权用户“tomcat”运行的Tomcat(我知道还有其他解决方案,但我有兴趣使这个工作)。为此,我有以下套接字单元(“port”设置已在server.xml上更改为80):
/etc/sytemd/system/tomcat.socket
[Unit]
Description=Tomcat server socket
[Socket]
ListenStream=80
以下服务单位:
/etc/sytemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=CATALINA_PID=/opt/tomcat/9.0.2/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/9.0.2
ExecStart=/opt/tomcat/9.0.2/bin/startup.sh
ExecStop=/opt/tomcat/9.0.2/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
[Install]
Requires=tomcat.socket
运行sudo systemctl start tomcat.service
时,该命令不会输出任何错误消息,但systemctl status tomcat.service
显示失败状态:
tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/etc/systemd/system/tomcat.service; static; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2017-12-19 21:46:53 -05; 45min ago
Process: 10207 ExecStop=/opt/tomcat/9.0.2/bin/shutdown.sh (code=exited, status=1/FAILURE)
Process: 10175 ExecStart=/opt/tomcat/9.0.2/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 10189 (code=exited, status=0/SUCCESS)
从下面的journald
日志中可以看出,Tomcat确实已启动,但shutdown.sh
会立即运行并退出。显然,Tomcat仍尝试在端口80上创建一个套接字,该套接字失败,因为用户tomcat
不是超级用户,并立即退出。
systemd[1]: Starting Apache Tomcat Web Application Container...
startup.sh[10175]: Existing PID file found during start.
startup.sh[10175]: Removing/clearing stale PID file.
startup.sh[10175]: Tomcat started.
systemd[1]: Started Apache Tomcat Web Application Container.
sudo[10172]: pam_unix(sudo:session): session closed for user root
shutdown.sh[10207]: PID file found but no matching process was found. Stop aborted.
systemd[1]: tomcat.service: Control process exited, code=exited status=1
systemd[1]: tomcat.service: Unit entered failed state.
systemd[1]: tomcat.service: Failed with result 'exit-code'.
如果Tomcat 9能够从systemd接收预初始化的套接字,我该如何使其工作?或者Tomcat无法这样做?