zookeeper的启动脚本放在:/etc/init.d/zookeeper
使用service zookeeper start 命令,输出提示找不到java命令。
通过在终端打印 env 输出的内容里面,设置了Java的环境变量,查看service 命令,找到了答案,见下图:
相关内容就是下面两段:
service runs a System V init script in as predictable environment as possible, removing most environment variables and with current working directory set to /.
ENVIRONMENT
LANG, TERM
The only environment variables passed to the init scripts.
由上可知,使用service命令时,仅有 LANG, TERM 两个环境变量能使用。
暂时的解决办法,直接使用路径来启动:
/etc/init.d/zookeeper start
另一种情况,如果使用的是:systemctl start zookeeper,还是会报找不到java的错误。
这和上面的原因类似,systemctl命令是用来管理systemd进程的。而systemd默认读取的默认路径,在systemd.exec中有明确的说明:
Environment Variables Set or Propagated by the Service Manager
The following environment variables are propagated by the service manager or generated internally for each invoked process:
$PATH
Colon-separated list of directories to use when launching executables. systemd uses a fixed value of "
" in the system manager. When compiled for systems with "unmerged /usr/local/sbin
:/usr/local/bin
:/usr/sbin
:/usr/bin
/usr/
" (/bin
is not a symlink to /usr/bin
), ":
" is appended. In case of the user manager, a different path may be configured by the distribution. It is recommended to not rely on the order of entries, and have only one program with a given name in /sbin
:/bin
$PATH
.
systemd只会调用/usr/local/sbin
:/usr/local/bin
:/usr/sbin
:/usr/bin 这些目录下的命令,因为这些目录下没有java命令,所以就会报前面提到的那个找不到java命令的错误。
讲到这里,大家应该知道怎么解决该问题吧,最简单的,就是在以上任何一个目录下,创建一个java命令的软连接,比如:
ln java命令所在绝对路径 java
除了以上方法,还可以在自定义的servicename.service文件中,增加环境变量,这里就不再展开叙述了,感兴趣的去搜下添加环境变量的内容即可。