为什么我的命名管道输入命令行在调用时才挂起? [英] Why my named pipe input command line just hangs when it is called?

查看:73
本文介绍了为什么我的命名管道输入命令行在调用时才挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据答案:

  1. 写为后台进程的标准输入
  2. 访问bash命令行args $ @ vs $ *
  3. 将命令发送到后台进程
  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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆