gpt4 book ai didi

linux - 为什么我的命名管道输入命令行在被调用时挂起?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:16 25 4
gpt4 key购买 nike

为什么我的命名管道输入命令行在调用时挂起?

根据答案:

  1. Writing to stdin of background process
  2. Accessing bash command line args $@ vs $*
  3. Send command to a background process
  4. Can I redirect output to a log file and background a process at the same time?

我写了两个 shell 脚本来与我的游戏服务器通信。并且在我第一次做的时候就成功了。因为他们不再工作了。每次我执行 ./send.sh 命令 时,命令行都会挂起,直到我按下 Ctrl+C

当我直接执行 echo commad >/tmp/srv-input

时,它也会挂起并且什么都不做

脚本

它会启动服务器并将其配置为在后台运行时读取/接收我的命令:

start_czero_server.sh

#!/bin/sh

# Go to the game server application folder where the game application `hlds_run` is
cd /home/user/Half-Life
pkill -f hlds

# Set up a pipe named `/tmp/srv-input`
rm /tmp/srv-input
mkfifo /tmp/srv-input

cat > /tmp/srv-input &
echo $! > /tmp/srv-input-cat-pid

# Start the server reading from the pipe named `/tmp/srv-input`
# And also output all its console to the file `/home/user/Half-Life/my_logs.txt`
cat /tmp/srv-input | ./hlds_run -console -game czero +port 27015 > my_logs.txt 2>&1 &

# Successful execution
exit 0

第二个脚本只是一个包装器,它允许我轻松地将命令发送到我的服务器:

发送.sh

#!/bin/sh
echo "$@" > /tmp/srv-input

# Successful execution
exit 0

现在每次我想向我的服务器发送命令时,我只需在终端上执行:

./send.sh mp_timelimit 30

我总是保持另一个打开的终端打开只是为了听我的服务器服务器控制台。为此,只需使用带有 -f 标志的 tail 命令来跟踪我的服务器控制台输出:

./tail -f /home/user/Half-Life/my_logs.txt

最佳答案

最好让 hlds_run 直接从管道中读取,而不是让 cat 通过管道将其读入。

尝试

./hlds_run … > my_logs.txt 2>&1 < /tmp/srv-input &

代替

cat /tmp/srv-input | ./hlds_run …

关于linux - 为什么我的命名管道输入命令行在被调用时挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42193081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com