在 Ubuntu 16.04 中启动服务时出错:

在 Ubuntu 16.04 中启动服务时出错:

我创建了一个像这样的 systemd 单元文件:

[Unit]
Description=Menu Core Prices Update Daemon
After=network.target

[Service]
Environment="APP_CP=/opt/menu/menu-core-price-update"
Environment="JAVA_HOME=/usr/java/latest"
Environment="APP_NAME=menu-core-prices-update-0.0.1-SNAPSHOT.jar"
Environment="LOGPATH=/var/log/menu/menu-core-price-update"
ExecStartPre=/bin/mkdir -pm 0755 ${LOGPATH}
ExecStart=/bin/bash -c "$JAVA_HOME/bin/java -jar ${APP_NAME}"
PIDFile=/run/menu/menu-core-price-update%i.pid
Restart=on-abort
RuntimeDirectory=menu-core-price-update
RuntimeDirectoryMode=755
WorkingDirectory=/opt/menu/menu-core-price-update

[Install]
WantedBy=multi-user.target

但是当我启用该服务时,出现以下错误:

 systemctl status menu-core-prices-update.service
    ● menu-core-prices-update.service - menu Core Prices Update Daemon
       Loaded: loaded (/usr/lib/systemd/system/menu-core-prices-update.service; enabled; vendor preset: enabled)
       Active: failed (Result: exit-code) since Sat 2018-07-21 06:17:43 UTC; 6min ago
      Process: 5154 ExecStartPre=/bin/mkdir -pm 0755 ${LOGPATH} (code=exited, status=200/CHDIR)

并且没有登录/var/log/

答案1

您误解了单元文件中这一行的用途:

Environment="LOGPATH=/var/log/menu/menu-core-price-update"

此属性用于设置 Java 应用程序要登录的目录位置(如果它选择)。但是,如果应用程序没有通过 log4j 或它使用的任何记录器生成日志,则此位置将不会有日志。

使用 systemd 服务,应用程序服务的所有日志记录都是通过 Journald 完成的。要查看您的应用程序日志:

$ journalctl -u menu-core-price-update.service

例子

这是我的 sshd 日志的前 10 行

$ journalctl -u sshd.service -n 10
-- Logs begin at Tue 2018-07-17 16:01:01 EDT, end at Sat 2018-07-21 03:05:37 EDT. --
Jul 21 00:55:04 centos7 sshd[9059]: Connection closed by 127.0.0.1 port 33718 [preauth]
Jul 21 00:55:04 centos7 sshd[9067]: Connection closed by 127.0.0.1 port 33720 [preauth]
Jul 21 00:55:08 centos7 sshd[9078]: pam_unix(sshd:auth): authentication failure; logname= uid=
Jul 21 00:55:10 centos7 sshd[9078]: Failed password for user1 from 127.0.0.1 port 33722 ssh2
Jul 21 00:55:10 centos7 sshd[9078]: error: maximum authentication attempts exceeded for user1
Jul 21 00:55:10 centos7 sshd[9078]: Disconnecting: Too many authentication failures [preauth]
Jul 21 00:55:21 centos7 sshd[9110]: Connection closed by 127.0.0.1 port 33724 [preauth]
Jul 21 00:55:21 centos7 sshd[9118]: Connection closed by 127.0.0.1 port 33726 [preauth]
Jul 21 00:55:23 centos7 sshd[9129]: Accepted password for user1 from 127.0.0.1 port 33728 ssh2
Jul 21 02:24:41 centos7 sshd[9433]: Accepted publickey for vagrant from 10.0.2.2 port 50662 ss

参考

相关内容