Java

Java 服務在 CLI 上執行時無法使用 SystemD 啟動

  • January 15, 2018

我有一個使用 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

Metabase 不知道如何解釋>>您在命令行中提供的字元串。它將它解釋為一個命令,但它似乎實際上並不是一個有效的命令。

看來您打算將程序的輸出重定向到某個地方。但是您不能使用>>systemd 中的其他 shell 結構來做到這一點,因為沒有使用任何 shell。因此,它作為命令行參數傳遞給 Metabase。

相反,您應該使用StandardOutput=andStandardError=來重定向標準輸出和標準錯誤,就像您已經擁有的那樣。

引用自:https://serverfault.com/questions/892172