在 GNU/Linux 上将 Java httpd 服务器作为服务运行

在 GNU/Linux 上将 Java httpd 服务器作为服务运行

我有一个内置 Grizzly http 服务器的 Java 应用。我无法运行它,无论是在前台运行还是作为 systemd 的服务运行。

启动后它就自动关闭了。我是不是漏掉了什么?

如果我从 bash 运行,shell 保持打开状态-一切正常:

[root@Test-LAPP02 RL_acskidd] java -jar RestListener-3.3-jar-with-dependencies.jar    
    Oct 24, 2017 5:00:37 PM org.glassfish.grizzly.http.server.NetworkListener start
        INFO: Started listener bound to [192.168.18.41:1177]
        Oct 24, 2017 5:00:37 PM org.glassfish.grizzly.http.server.HttpServer start
        INFO: [HttpServer] Started.
        Oct 24, 2017 5:00:38 PM org.glassfish.grizzly.http.server.NetworkListener start
        INFO: Started listener bound to [192.168.18.41:1188]
        Oct 24, 2017 5:00:38 PM org.glassfish.grizzly.http.server.HttpServer start
        INFO: [HttpServer-1] Started.

但是,如果我尝试将其作为 systemd 服务启动(或者甚至在命令末尾使用 & 在前台启动),它会在启动后立即关闭:

[root@Test-LAPP02 RL_acskidd]# systemctl start rl_acskidd
Oct 24 17:08:29 Test-LAPP02 Rest Listener ACSKIDD[1826]: Oct 24, 2017 5:08:29 PM org.glassfish.grizzly.http.server.NetworkListener start
Oct 24 17:08:29 Test-LAPP02 Rest Listener ACSKIDD[1826]: INFO: Started listener bound to [192.168.18.41:1177]
Oct 24 17:08:29 Test-LAPP02 Rest Listener ACSKIDD[1826]: Oct 24, 2017 5:08:29 PM org.glassfish.grizzly.http.server.HttpServer start
Oct 24 17:08:29 Test-LAPP02 Rest Listener ACSKIDD[1826]: INFO: [HttpServer] Started.
Oct 24 17:08:31 Test-LAPP02 Rest Listener ACSKIDD[1826]: Oct 24, 2017 5:08:31 PM org.glassfish.grizzly.http.server.NetworkListener start
Oct 24 17:08:31 Test-LAPP02 Rest Listener ACSKIDD[1826]: INFO: Started listener bound to [192.168.18.41:1188]
Oct 24 17:08:31 Test-LAPP02 Rest Listener ACSKIDD[1826]: Oct 24, 2017 5:08:31 PM org.glassfish.grizzly.http.server.HttpServer start
Oct 24 17:08:31 Test-LAPP02 Rest Listener ACSKIDD[1826]: INFO: [HttpServer-1] Started.
Oct 24 17:08:31 Test-LAPP02 Rest Listener ACSKIDD[1826]: Oct 24, 2017 5:08:31 PM org.glassfish.grizzly.http.server.NetworkListener shutdownNow
Oct 24 17:08:31 Test-LAPP02 Rest Listener ACSKIDD[1826]: INFO: Stopped listener bound to [192.168.18.41:1177]
Oct 24 17:08:31 Test-LAPP02 Rest Listener ACSKIDD[1826]: Oct 24, 2017 5:08:31 PM org.glassfish.grizzly.http.server.NetworkListener shutdownNow
Oct 24 17:08:31 Test-LAPP02 Rest Listener ACSKIDD[1826]: INFO: Stopped listener bound to [192.168.18.41:1188]
Oct 24 17:08:31 Test-LAPP02 systemd[1]: Received SIGCHLD from PID 1826 (java).
Oct 24 17:08:31 Test-LAPP02 systemd[1]: Child 1826 (java) died (code=exited, status=0/SUCCESS)
Oct 24 17:08:31 Test-LAPP02 systemd[1]: Child 1826 belongs to rl_acskidd.service
Oct 24 17:08:31 Test-LAPP02 systemd[1]: rl_acskidd.service: main process exited, code=exited, status=0/SUCCESS

这是我的单位文件:

[Unit]
Description=Rest Listener
After=network-online.target

[Service]
Type=service
User=nobody
SyslogIdentifier=Rest Listener
WorkingDirectory=/opt/RL_acskidd
ExecStart=/usr/bin/java -jar RestListener-3.3-jar-with-dependencies.jar

[Install]
WantedBy=multi-user.target

我用 Google 搜索了该问题,发现 Java 应用程序应该使用“作为服务运行”功能编写,但我还发现许多人仍然可以将其作为 systemd 单元运行。

我尝试了各种 ExecStart= 选项来模拟 shell,例如这个:

ExecStart=/bin/bash -c "/usr/bin/java -jar RestListener-3.3-jar-with-dependencies.jar"

但还是没运气。应该去哪里解决这个问题?谢谢解答!

答案1

systemd-analyze verify通过运行您的文件发现了您的问题,发现了以下错误:

systemd-analyze verify ./foo.service [/home/mark/tmp/foo.service:6] Failed to parse service type, ignoring: service

请查看并为您的服务man systemd.service选择正确的值。Type=

尝试启动服务后,请务必检查journalctl是否存在错误。journalctl -u your-service-name

答案2

这是瞎猜的,但用户是否可能nobody无法访问某些文件或无法打开监听端口?我看到在尝试程序时bash您使用了root用户,但在systemd单元文件中您使用了nobody

相关内容