为什么我的命名管道输入命令行在调用时才挂起? [英] Why my named pipe input command line just hangs when it is called?
问题描述
根据答案:
- Writing to stdin of background process
- Accessing bash command line args $@ vs $*
- Send command to a background process
- Can I redirect output to a log file and background a process at the same time?
我编写了两个Shell脚本来与游戏服务器通信.并在我第一次这样做的时候就工作了.由于它们,它们不再起作用.每次我执行./send.sh commands
时,命令行都会挂起,直到我按下Ctrl+C
.
I wrote two shell scripts to communicate with my game server. And worked the first time I did it. Since it them they do not work anymore. Every time I do ./send.sh commands
the command line hangs until I hit Ctrl+C
.
当我直接执行echo commamd > /tmp/srv-input
它会启动服务器并将其配置为在后台运行时读取/接收我的命令:
It does start the server and configure it to read/receive my commands while it run in background:
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
第二个脚本只是一个包装,使我可以轻松地将命令发送到服务器:
This second script it just a wrapper which allow me easily to send commands to the my server:
send.sh
#!/bin/sh
echo "$@" > /tmp/srv-input
# Successful execution
exit 0
现在,每次我想向服务器发送命令时,我都只需在终端上执行
Now every time I want to send a command to my server I just do on the terminal:
./send.sh mp_timelimit 30
我总是保持另一个打开的终端打开,只是为了收听我的服务器服务器控制台.为此,只需使用带有-f
标志的tail
命令来跟踪我的服务器控制台输出:
I always keep another open terminal open just to listen to my server server console. To do it just use the tail
command with the -f
flag to follow my server console output:
./tail -f /home/user/Half-Life/my_logs.txt
推荐答案
最好直接从管道中读取hlds_run
而不是直接将cat
管道中读取.
You would be better off just having hlds_run
read directly from the pipe instead of having cat
pipe it in.
尝试
./hlds_run … > my_logs.txt 2>&1 < /tmp/srv-input &
代替
cat /tmp/srv-input | ./hlds_run …
这篇关于为什么我的命名管道输入命令行在调用时才挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!