Systemd 未从包装器启动应用程序

Systemd 未从包装器启动应用程序

我为 Elasticsearch 和 systemd 服务文件创建了一个简单的包装器,由于某种原因,我无法从 systemd 启动 Elasticsearch,但我可以从包装器本身启动。

这是我的(非常简单的)包装器:

#!/bin/sh
SERVICE_NAME=elasticsearch
PATH_TO_APP="/opt/$SERVICE_NAME/bin/$SERVICE_NAME"
PID_PATH_NAME="/var/run/$SERVICE_NAME/$SERVICE_NAME.pid"
SCRIPTNAME=elasticsearch-wrapper.sh
ES_USER=$SERVICE_NAME
ES_GROUP=$SERVICE_NAME
SUDO="sudo -u $SERVICE_NAME"

case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
        mkdir $(dirname $PID_PATH_NAME) > /dev/null 2>&1 || true
            chown $ES_USER $(dirname $PID_PATH_NAME)
            $SUDO $PATH_TO_APP -d -p $PID_PATH_NAME
        echo "Return code: $?"
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ..."
            kill -15 $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill -15 $PID;
        sleep 1;
            echo "$SERVICE_NAME stopped ...";
            rm -rf $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            mkdir $(dirname $PID_PATH_NAME) > /dev/null 2>&1 || true
            chown $ES_USER $(dirname $PID_PATH_NAME)
            $SUDO $PATH_TO_APP -d -p $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
    exit 3
    ;;
esac

这是我的elasticsearch.service文件:

[Unit]
Description=ElasticSearch Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
Type=simple
#ExecStart=/opt/elasticsearch/bin/elasticsearch -d
ExecStart=/opt/elasticsearch/bin/elasticsearch-wrapper.sh start
ExecStop=/opt/elasticsearch/bin/elasticsearch-wrapper.sh stop
ExecReload=/opt/elasticsearch/bin/elasticsearch-wrapper.sh restart
#ExecStop=/opt/elasticsearch/bin/elasticsearch-stop.sh
LimitNOFILE=65536
LimitMEMLOCK=infinity
User=root
StandardOutput=journal+console

systemd status elasticsearch这是尝试运行后的结果:

● elasticsearch.service - ElasticSearch Server
   Loaded: loaded (/etc/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Mar 23 17:38:30 ip-10-96-13-64 systemd[1]: Started ElasticSearch Server.
Mar 23 17:38:30 ip-10-96-13-64 elasticsearch-wrapper.sh[2866]: Starting elasticsearch ...
Mar 23 17:38:30 ip-10-96-13-64 sudo[2872]:     root : unable to resolve host ip-10-96-13-64
Mar 23 17:38:30 ip-10-96-13-64 elasticsearch-wrapper.sh[2866]: sudo: unable to resolve host ip-10-96-13-64
Mar 23 17:38:30 ip-10-96-13-64 sudo[2872]:     root : TTY=unknown ; PWD=/ ; USER=elasticsearch ; COMMAND=/opt/elasticsearch/bin/elasticsearch -d -p /var/run/elasticsearch/elasticsearch.pid
Mar 23 17:38:30 ip-10-96-13-64 sudo[2872]: pam_unix(sudo:session): session opened for user elasticsearch by (uid=0)
Mar 23 17:38:30 ip-10-96-13-64 sudo[2872]: pam_unix(sudo:session): session closed for user elasticsearch
Mar 23 17:38:30 ip-10-96-13-64 elasticsearch-wrapper.sh[2866]: Return code: 0
Mar 23 17:38:30 ip-10-96-13-64 elasticsearch-wrapper.sh[2866]: elasticsearch started ...
Mar 23 17:38:30 ip-10-96-13-64 elasticsearch-wrapper.sh[2895]: elasticsearch is not running ...

我放了一个调试语句,在启动 elasticsearch 时返回“返回代码”,正如您在中看到的那样,systemctl status它正在返回0。我甚至在执行后立即跟踪了 elasticsearch 的日志systemctl start elasticsearch,但什么也没有,日志中什么都没有;就像它甚至没有启动 elasticsearch 一样。

我不知道为什么会发生这种情况,所以作为最后的手段,我在这里发布它,看看是否有人可以帮助我并指出这里发生了什么。重申一下,直接从包装器启动/停止/重新启动 elasticsearch 可以正常工作,没有任何问题,但使用 systemd 则不起作用。

编辑:我使用包装器的原因是因为根据他们的文档,您必须执行kill该过程。此外,我更希望有该ExecReload选项。

EDIT2:为了确认 elasticsearch 中没有stop选项,下面是输出elasticsearch --help

USER# ./elasticsearch --help
starts elasticsearch

Option                Description                                              
------                -----------                                              
-E <KeyValuePair>     Configure a setting                                      
-V, --version         Prints elasticsearch version information and exits       
-d, --daemonize       Starts Elasticsearch in the background                   
-h, --help            show help                                                
-p, --pidfile <Path>  Creates a pid file in the specified path on start        
-q, --quiet           Turns off standard ouput/error streams logging in console
-s, --silent          show minimal output                                      
-v, --verbose         show verbose output

答案1

好的,因此根据 iwaseatenbyagrue 提供的信息,PIDFile只有当我将从 更改为 时,添加才有效Typesimple所以forking现在它可以按预期工作!

我现在的样子如下elasticsearch.service

[Unit]
Description=ElasticSearch Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
#ExecStart=/opt/elasticsearch/bin/elasticsearch -d
ExecStart=/opt/elasticsearch/bin/elasticsearch-wrapper.sh start
ExecStop=/opt/elasticsearch/bin/elasticsearch-wrapper.sh stop
ExecReload=/opt/elasticsearch/bin/elasticsearch-wrapper.sh restart
#ExecStop=/opt/elasticsearch/bin/elasticsearch-stop.sh
PIDFile=/var/run/elasticsearch/elasticsearch.pid
LimitNOFILE=65536
LimitMEMLOCK=infinity
User=root
StandardOutput=journal+console

答案2

在大多数情况下(以及您的单元文件的情况),systemd 需要直接跟踪程序的 PID。

添加包装器意味着 systemd 可以看到包装器的 PID,但无法跟踪 ES 的 PID。

您可以在输出中看到这一点:

Mar 23 17:38:30 ip-10-96-13-64 elasticsearch-wrapper.sh[2866]: Return code: 0

您的包装器正在干净地退出,因此 systemd 得出结论认为没有任何内容正在运行。

我强烈建议您将任何必要的设置功能移至使用 systemd ExecStartPre,并直接从 systemd 运行 ES。

如果您确实必须使用包装器,那么您应该确保 ES 写入 PID 文件,并使用 向 systemd 提供它的路径PIDFile

不过,我认为你最好有类似的东西:

[Unit]
Description=ElasticSearch Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
Type=simple
User=elasticsearch
Group=elasticsearch
ExecStartPre=-mkdir /opt/elasticsearch/data
ExecStart=/opt/elasticsearch/bin/elasticsearch OPTIONS
ExecStop=/opt/elasticsearch/bin/elasticsearch stop
LimitNOFILE=65536
LimitMEMLOCK=infinity
StandardOutput=journal

相关内容