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

Why my named pipe input command line just hangs when it is called?

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

基于答案:

  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


脚本

它确实启动服务器并将其配置为 read/receive 我的命令,同时它 运行 在后台:

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

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

尝试

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

而不是

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