我有 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/