systemd 启动 logstash 报错 找不到 JAVA_HOME

问题

我们通过logstash 的安装包进行安装好了logstash,然后我们通过systemd进行管理启动,我们发现有如下报错的。

Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME.

看这个问题是没有找到 JAVA_HOME的。但是我们是已经配置好了 JAVA_HOME(在 /etc/profile) 的,我们在终端进行echo $JAVA_HOME,是可以获取到信息的。

logstash 的 systemd的配置文件

[Unit]
Description=logstash

[Service]
Type=simple
User=logstash
Group=logstash
# Load env vars from /etc/default/ and /etc/sysconfig/ if they exist.
# Prefixing the path with '-' makes it try to load, but if the file doesn't
# exist, it continues onward.
ExecStart=/home/logstash/bin/logstash "--path.settings" "/home/logstash/config"
Restart=always
WorkingDirectory=/
Nice=19
LimitNOFILE=16384

[Install]
WantedBy=multi-user.target

问题原因

systemd 管理的服务的环境变量是不会去加载 /etc/profile的,我们可以在 service 文件中配置 Environment或者EnvironmentFile 来指定 systemd 管理的每个服务单独的环境变量。

解决办法

方法一

我们在/usr/lib/systemd/system/logstash.service配置的 Service
加上 Environment="JAVA_HOME=/usr/local/java/jdk1.8.0_151",也就是使用 Environment 进行指定环境变量 JAVA_HOME的值。

方法二

logstash/bin/logstash.lib.sh 或者 logstash/bin/logstash 加上个 source /etc/profile.

原文地址:https://www.cnblogs.com/operationhome/p/12463779.html