【问题标题】:Why my named pipe input command line just hangs when it is called?为什么我的命名管道输入命令行在被调用时只是挂起?
【发布时间】:2017-06-30 18:58:41
【问题描述】:

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

根据答案:

  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 commands 时,命令行都会挂起,直到我点击Ctrl+C

当我直接echo commamd > /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

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

send.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

【问题讨论】:

    标签: linux shell unix pipe named-pipes


    【解决方案1】:

    您最好让hlds_run 直接从管道中读取,而不是让cat 管道输入。

    试试

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

    代替

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

    【讨论】:

      猜你喜欢
      • 2013-01-22
      • 1970-01-01
      • 2019-08-04
      • 2011-12-25
      • 2016-03-25
      • 2010-09-15
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      相关资源
      最近更新 更多