linux - echo 消息没有出现在 systemd 的终端上

标签 linux console fedora tty systemd

我有 systemd 服务,比如 xyzWarmup.service。

这是服务文件

[Unit]
Description=Xyz agent.
After=fooAfter.service
Before=fooBefore1.service
Before=fooBefore2.service

[Service]
# During boot the xyz.sh script reads input from /dev/console.  If the user
# hits <ESC>, it will skip waiting for xyz and abc to startup.
Type=oneshot
StandardInput=tty
StandardOutput=tty
ExecStart=/usr/bin/xyz.sh start

RemainAfterExit=True
ExecStop=/usr/bin/xyz.sh stop

[Install]
WantedBy=multi-user.target

以下是xyz.sh的部分

#! /bin/bash                                                                                                                                                                                               
#                                                              
### BEGIN INIT INFO                                                                                                                                                                                         
# Required-Stop: Post                                                                                                                                                                                       
### END INIT INFO                                                                                                                                                                                           

XYZ=/usr/bin/Xyz
prog="Xyz"
lockfile=/var/lock/subsys/$prog
msg="Completing initialization"

start() {
     # Run wfw in background                                                                                                                                                                                 
    ulimit -c 0
    # wfw has a default timeout of 10 minutes - just pick a large value                                                                                                                                     
    wfw -t 3600 xyz abc >/dev/null 2>&1 &
    PID=$!

    # Display the message here after spawning wfw so Esc can work                                                                                                                                           
    echo -n $"$msg (press ESC to skip): "
    while [ 1 ]; do
        read -s -r -d "" -N 1 -t 0.2 CHAR || true
        if [ "$CHAR" = $'\x1B' ]; then
            kill -9 $PID 2>/dev/null
            # fall through to wait for process to exit                                                                                                                                                      
        fi

        STATE="`ps -p $PID -o state=`"
        if [ "$STATE" = ""  ]; then 
            # has exited                                                                                                                                                                                    
            wait $PID 2>/dev/null
            if [ $? -eq 0 ]; then
                echo "[ OK ]" 
                echo
                exit 0
            else
                echo "[ FAILED ]"
                echo "This is failure"  
                               exit 1
            fi
        fi
    done
}

当这个脚本在引导期间运行时,我看到来自脚本的以下消息

 Completing initialization (press ESC to skip): 

更新: 这是我在上一行之后看到的附加输出

[[  OK  ] Started Xyz agent.\n'

如果你仔细看,有 2 个左方括号 ('['),由此看来 systemd 正在覆盖日志消息。第一个“[”来自初始化脚本的“[OK]”。有人可以更好地解释这一点吗?

我在屏幕上没有看到“[OK]”或“[FAILED]”。

当我在 Fedora14 中使用这个脚本作为 initscript 时,我曾经看到过这些消息。曾经,我已经转向 systemd。我已经开始看到这个问题。

systemd 版本是:systemd-201-2.fc18.9.i686 和 systemd.default_standard_output=tty

请帮忙。

最佳答案

在我看来,您的问题是脚本从未附加到 TTY。显示输出是因为您在脚本中进行了硬编码以转到 /dev/console。使用 StandardInput=tty,systemd 会等待直到 tty 可用,并且它可能已经在使用中。您的脚本只是坐在那里,没有连接到无限循环中的输入。您可以尝试 StandardInput=tty-force,我敢打赌这会奏效,尽管我不确定 else 可能会出现什么问题。

就我个人而言,我想我可能会回过头来重新考虑整个方法。听起来您希望 Bootstrap 完全阻止此服务,但您可以通过点击 escape 来跳过。也许有更好的方法?

关于linux - echo 消息没有出现在 systemd 的终端上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34335374/

相关文章:

linux - 如何将命令的输出直接复制到剪贴板?

java - System.setOut 与 PipedOutputStream

symfony2 缓存清除 --env=prod 不打印任何内容

linux - 在 Fedora 8 Core 上运行 startupscript

regex - 在文本文件中按列对唯一元素进行排序

linux - 分隔符”、空格和 Linux 中的 bash 脚本

java - 如何使用 32 位 Java 8 嵌入式 JRE 访问 64 位 JNI 库

windows - 在控制台中显示希伯来语文本

java - 如何使用 OpenJDK 在 Fedora 17 上编译为 Java 6

centos - 为什么 rpmbuild 需要这些 C++ 库,从而导致此错误?