# 解决Elasticsearch SQL命令行启动报错 ./x-pack-env: No such file or directory

> 作者：zning（https://ning.ae） · 发布：2020/07/19 16:54 下午 (UTC+8) · 原文：https://zning.me/15951488866193.html

今天在云服务器上，通过下载源码包完成有认证配置的Elasticsearch部署后，准备执行如下命令进入Elasticsearch SQL进行一些SQL语句的操作：

```shell
bin/elasticsearch-sql-cli uri=http://elastic:ESabc+2333@10.66.66.2:9200/
```

但执行后，发现使用`bin/elasticsearch-sql-cli`登录时，出错提示`./x-pack-env: No such file or directory`，莫名其妙，遂即查看下该脚本，内容如下：

```shell
#!/bin/bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.

source "`dirname "$0"`"/elasticsearch-env

source "`dirname "$0"`"/x-pack-env

CLI_JAR=$(ls $ES_HOME/bin/elasticsearch-sql-cli-*.jar)

exec \
  "$JAVA" \
  -jar "$CLI_JAR" \
  "$@"
```

其中有效行第二行的<code>source "\`dirname "$0"\`"/x-pack-env</code>看来执行起来有问题，估计是在依赖导入生效时找不到路径。经过一番折腾，解决了该问题，即将该行替换为如下命令：

```shell
# source "`dirname "$0"`"/x-pack-env
source /usr/share/elasticsearch/bin/x-pack-env
```

即将引入依赖的路径写死即可，使用时请根据实际路径修改`x-pack-env`的指向路径。

修改完后，再用刚才的命令启动Elasticsearch SQL，正常进入，完美结局。

后来又仔细查找了下资料，发现该问题是Elasticsearch 7.4版本的一个小bug，在Elasticsearch 7.8的包内，`bin/elasticsearch-sql-cli`这个脚本内容已经修改为：

```shell
#!/bin/bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.

source "`dirname "$0"`"/elasticsearch-env

source "$ES_HOME"/bin/x-pack-env

CLI_JAR=$(ls "$ES_HOME"/bin/elasticsearch-sql-cli-*.jar)

exec \
  "$JAVA" \
  "$XSHARE" \
  -jar "$CLI_JAR" \
  "$@"
```

也就是说，如果Elasticsearch 7.8安装时，操作系统环境变量内`$ES_HOME`能够正确设置的话，就可以正常启动Elasticsearch SQL了。这样比直接修改产品本身脚本来说要规范的多了。

## 参考文献

1. [Elasticsearch 7 SQL CLI : ./x-pack-env: No such file or directory error - Stack Overflow](https://stackoverflow.com/questions/59420831/elasticsearch-7-sql-cli-x-pack-env-no-such-file-or-directory-error)
