【问题标题】:echo message not coming on terminal with systemd回显消息没有出现在 systemd 的终端上
【发布时间】:2015-12-17 13:07:31
【问题描述】:

我有 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 正在覆盖日志消息。第一个“[”来自 initscript 的“[ OK ]”。有人可以更好地解释一下吗?

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

当我在 Fedora14 中将此脚本用作 initscript 时,我曾经看到这些消息。有一次,我已经转移到 systemd。我已经开始看到这个问题了。

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

请帮忙。

【问题讨论】:

标签: linux console fedora tty systemd


【解决方案1】:

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

就个人而言,我想我可能会回去重新考虑整个方法。听起来您希望引导程序完全阻止此服务,但让您通过点击退出来跳过。也许有更好的方法?

【讨论】:

  • 引导不会阻塞,它只是等到 wfw 进程自动退出或者在此之前如果有人按 ESC(在这种情况下我们会杀死 wfw 进程)
  • 还有许多其他的 intiscripts 也在这个脚本之前和之后运行,它们也没有硬编码到 /dev/console 但即使这样所有的回显消息都会出现在屏幕上.我怀疑一旦 wfw 退出或被杀,就会发生一些事情。我什至尝试通过硬编码添加更多回显消息以转到 /dev/console 并且它们都出现在 if 语句之上。
  • 这里还有一点需要注意的是,我从 while 循环内部收到回显消息,如果它们在我提到的 if 语句之前。看起来好像只要 wfw 退出/被杀死,我就无法连接到 tty
  • 在删除 /dev/console 时,我仍然看到该消息。
  • 我已经准备好按照你说的做出改变,但我想先知道这样做的原因。我想从根本上解决这个问题。从侧面观察,如果在系统配置文件中我更改 systemd.default_standard_output=console。丢失的消息开始出现。
猜你喜欢
  • 1970-01-01
  • 2022-07-11
  • 2016-06-02
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 2013-11-25
  • 2015-06-12
相关资源
最近更新 更多