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

送分小仙女□ 提交于 2020-01-05 05:29:07

问题


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

Based on the answers:

  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?

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.

It also hangs and does nothing when I do directly echo commamd > /tmp/srv-input


The scripts

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

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

回答1:


You would be better off just having hlds_run read directly from the pipe instead of having cat pipe it in.

Try

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

Instead of

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


来源:https://stackoverflow.com/questions/42193081/why-my-named-pipe-input-command-line-just-hangs-when-it-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!