Systemd退出,状态=209/STDOUT

Systemd退出,状态=209/STDOUT,systemd,rhel,Systemd,Rhel,场景 我有一个systemd文件,我想有条件地运行它,但前提是环境变量ISCAPTUREPOD设置为true 我有一个容器,它在启动时运行两个服务,但有一个特殊的场景,我只希望其中一个服务运行。我通过Kubernetes传递了一个环境变量,我想用它来控制第二个服务是否启动。我有一个名为iscapturepod.sh的脚本,它检查环境变量,它是ExecStartPre语句的一部分。我希望当环境变量ISCAPTUREPOD设置为“True”时脚本成功,如果它不存在或设置为“True”以外的值时脚本失

场景

我有一个systemd文件,我想有条件地运行它,但前提是环境变量ISCAPTUREPOD设置为true

我有一个容器,它在启动时运行两个服务,但有一个特殊的场景,我只希望其中一个服务运行。我通过Kubernetes传递了一个环境变量,我想用它来控制第二个服务是否启动。我有一个名为iscapturepod.sh的脚本,它检查环境变量,它是ExecStartPre语句的一部分。我希望当环境变量ISCAPTUREPOD设置为“True”时脚本成功,如果它不存在或设置为“True”以外的值时脚本失败

问题:

[Unit]
Description=Moloch Capture
After=network.target

[Service]
Type=simple
Restart=on-failure
StandardOutput=tty
ExecStartPre= /bin/sh -c '/data/moloch/bin/iscapturepod.sh'
ExecStart=/bin/sh -c '/data/moloch/bin/moloch-capture -c MOLOCH_INSTALL_DIR/etc/config.ini ${OPTIONS} >> /data/moloch/logs/capture.log 2>&1'
LimitCORE=infinity
LimitMEMLOCK=infinity

[Install]
WantedBy=multi-user.target
无论我做什么,ExecStartPre都会失败。我甚至试着让脚本说
exit 0
。这是整个剧本中唯一的一件事,只是因为我想强行取得成功。Systemd仍然失败,状态为209/STDOUT

莫洛克捕获服务:

[Unit]
Description=Moloch Capture
After=network.target

[Service]
Type=simple
Restart=on-failure
StandardOutput=tty
ExecStartPre= /bin/sh -c '/data/moloch/bin/iscapturepod.sh'
ExecStart=/bin/sh -c '/data/moloch/bin/moloch-capture -c MOLOCH_INSTALL_DIR/etc/config.ini ${OPTIONS} >> /data/moloch/logs/capture.log 2>&1'
LimitCORE=infinity
LimitMEMLOCK=infinity

[Install]
WantedBy=multi-user.target
脚本

#!/bin/bash

# This script checks to see whether this pod is or is not a capture pod
# Kubernetes will pass the ISCAPTUREPOD variable as an environment variable with
# value True to  those pods meant for capture and False for the viewer pod.
# This allows us to only use one container for both the viewer and capture pods
# The molochcapture service will run this in an ExecStartPre statement. If it
# throws an error this will prevent the molochcapture service from starting

if [[ ! -z "${ISCAPTUREPOD}" ]]; then  
  if [[ "${ISCAPTUREPOD}" == "True" ]]; then    
    echo This is a capture pod
    exit 0
  else    
    echo This is not a capture pod 1>&2    
    exit 1  
  fi
else  
  echo This is not a capture pod 1>$2  
  exit 1
fi
根据0,应该是成功的。但是,即使我将脚本更改为仅退出0,我仍然得到:

[root@sensor1 /]# systemctl status molochcapture
● molochcapture.service - Moloch Capture
   Loaded: loaded (/usr/lib/systemd/system/molochcapture.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Mon 2019-01-21 12:58:20 UTC; 1s ago
  Process: 281 ExecStartPre=/bin/sh -c /data/moloch/bin/iscapturepod.sh (code=exited, status=209/STDOUT)

Jan 21 12:58:20 sensor1.lan systemd[1]: Failed to start Moloch Capture.
Jan 21 12:58:20 sensor1.lan systemd[1]: Unit molochcapture.service entered failed state.
Jan 21 12:58:20 sensor1.lan systemd[1]: molochcapture.service failed.
Jan 21 12:58:20 sensor1.lan systemd[1]: molochcapture.service holdoff time over, scheduling restart.
Jan 21 12:58:20 sensor1.lan systemd[1]: Stopped Moloch Capture.
Jan 21 12:58:20 sensor1.lan systemd[1]: start request repeated too quickly for molochcapture.service
Jan 21 12:58:20 sensor1.lan systemd[1]: Failed to start Moloch Capture.
Jan 21 12:58:20 sensor1.lan systemd[1]: Unit molochcapture.service entered failed state.
Jan 21 12:58:20 sensor1.lan systemd[1]: molochcapture.service failed.

我已经手动检查了脚本是否正常工作,并且没有问题。Kubernetes正在按预期传递环境变量,脚本返回“这是一个捕获吊舱”。我想这可能与systemd无法访问标准输出有关,但当时我尝试退出0,但仍然失败。

我从通用Moloch模板获得了服务文件并对其进行了修改。我没有注意到它有一行
StandardOutput=tty
。当我注意到我意识到有多种类型的StandardOutput可以为服务文件选择时。它们在这里有很好的记录:。将值从tty更改为inherit为我解决了问题

问题是我试图输出到一个不存在的TTY行,这是导致抛出错误的原因

我的问题变得更加复杂,因为systemd没有从容器继承环境,因此脚本将准确地显示此失败