![Java 服务在 CLI 上运行时无法通过 SystemD 启动](https://linux22.com/image/716112/Java%20%E6%9C%8D%E5%8A%A1%E5%9C%A8%20CLI%20%E4%B8%8A%E8%BF%90%E8%A1%8C%E6%97%B6%E6%97%A0%E6%B3%95%E9%80%9A%E8%BF%87%20SystemD%20%E5%90%AF%E5%8A%A8.png)
我有一个使用 systemD 管理的元数据库服务。我可以通过直接在 CLI 上调用它来成功启动它:
/usr/bin/java -jar -Xmx512m /usr/local/sbin/metabase.jar
但是当尝试通过 systemD 服务启动它时,会出现错误:
Jan 14 20:45:59 sd-120866 systemd[1]: Started Metabase.
Jan 14 20:46:01 sd-120866 metabase[15432]: 01-14 20:46:01 #033[1mINFO metabase.util#033[0m :: Loading Metabase...
Jan 14 20:46:03 sd-120866 metabase[15432]: 01-14 20:46:03 #033[1mINFO util.encryption#033[0m :: DB details encryption is DISABLED for this Metabase instance.
Jan 14 20:46:03 sd-120866 metabase[15432]: See http://www.metabase.com/docs/latest/operations-guide/start.html#encrypting-your-database-connection-details-at-rest for more information.
Jan 14 20:46:06 sd-120866 metabase[15432]: #033[31mUnrecognized command: >>#033[0m
Jan 14 20:46:06 sd-120866 metabase[15432]: Valid commands are:
Jan 14 20:46:06 sd-120866 metabase[15432]: api-documentation []
Jan 14 20:46:06 sd-120866 metabase[15432]: #011 nil
Jan 14 20:46:06 sd-120866 metabase[15432]: help []
Jan 14 20:46:06 sd-120866 metabase[15432]: #011 nil
Jan 14 20:46:06 sd-120866 metabase[15432]: load-from-h2 [] [h2-connection-string]
Jan 14 20:46:06 sd-120866 metabase[15432]: #011 nil
Jan 14 20:46:06 sd-120866 metabase[15432]: migrate [direction]
Jan 14 20:46:06 sd-120866 metabase[15432]: #011 nil
Jan 14 20:46:06 sd-120866 metabase[15432]: profile []
Jan 14 20:46:06 sd-120866 metabase[15432]: #011 nil
Jan 14 20:46:06 sd-120866 metabase[15432]: reset-password [email-address]
Jan 14 20:46:06 sd-120866 metabase[15432]: #011 nil
Jan 14 20:46:06 sd-120866 metabase[15432]: version []
Jan 14 20:46:06 sd-120866 metabase[15432]: #011 nil
Jan 14 20:46:06 sd-120866 metabase[15432]: Some other commands you might find useful:
Jan 14 20:46:06 sd-120866 metabase[15432]: java -cp metabase.jar org.h2.tools.Shell -url jdbc:h2:/path/to/metabase.db
Jan 14 20:46:06 sd-120866 metabase[15432]: #011Open an SQL shell for the Metabase H2 DB
Jan 14 20:46:06 sd-120866 systemd[1]: metabase.service: Main process exited, code=exited, status=1/FAILURE
Jan 14 20:46:06 sd-120866 systemd[1]: metabase.service: Unit entered failed state.
Jan 14 20:46:06 sd-120866 systemd[1]: metabase.service: Failed with result 'exit-code'.
Jan 14 20:46:08 sd-120866 systemd[1]: metabase.service: Service hold-off time over, scheduling restart.
Jan 14 20:46:08 sd-120866 systemd[1]: Stopped Metabase.
我不知道哪里出了问题,更何况它以前还可以工作。这是 systemD 服务文件:
[Unit]
Description=Metabase
[Service]
ExecStart=/usr/bin/java -jar -Xmx512m /usr/local/sbin/metabase.jar >> /var/log/syslog
Restart=always
RestartSec=1500ms
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=metabase
[Install]
WantedBy=multi-user.target
答案1
Metabase 不知道如何解释您在命令行中提供的字符串>>
。它将其解释为命令,但它似乎实际上不是一个有效的命令。
看来您打算将程序的输出重定向到某个地方。但是您无法使用>>
systemd 中的其他 shell 构造来执行此操作,因为没有使用任何 shell。因此它作为命令行参数传递给 Metabase。
相反,您应该使用StandardOutput=
和StandardError=
重定向 stdout 和 stderr,就像您已经做的那样。