【发布时间】:2017-06-30 18:58:41
【问题描述】:
为什么我的命名管道输入命令行在被调用时只是挂起?
根据答案:
- 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。
当我直接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