我有一个以前运行的进程(process1.sh)在后台运行,其PID为1111(或其他任意数字)。我怎么能发送像command option1 option2
那样的PID到1111?发送命令到后台进程
我不要想要启动一个新的process1.sh!
我有一个以前运行的进程(process1.sh)在后台运行,其PID为1111(或其他任意数字)。我怎么能发送像command option1 option2
那样的PID到1111?发送命令到后台进程
我不要想要启动一个新的process1.sh!
命名管道是你的朋友。请参阅文章Linux Journal: Using Named Pipes (FIFOs) with Bash。
您无法将新参数发送到正在运行的进程。
但是,如果你正在实现这个过程或其过程,可以从一个管道采取参数,那么其他答案将有所帮助。
错,您可以使用命名管道来处理这类事情。 – 2011-05-13 22:03:23
@mu我这个问题读取像用户想要获得新的参数(argc/argv在C中或位置参数在bash中)。一旦程序启动,你不能给它新的参数。如果他是应用程序的作者,管道可以解决他的问题。他也可以实现一个TCP服务器......但是如果你正在谈论一个现有的命令 - 那么不会(如果他会说,我正在写这个应用程序,我想运行在BG ...) – nhed 2011-05-13 22:08:24
有在问题中有一些含糊不清的地方,但我没有阅读“发送类似命令option1 option2'”的意思,意思是在事后修改'argv',看起来Mohit只是想发送某种类型的消息给后台shell脚本。 – 2011-05-13 22:33:54
如果您不想限制为信号,您的程序必须支持一种Inter Process Communication方法。请参阅corresponding Wikipedia article。
一个简单的方法是让它监听Unix domain socket上的命令。
你可以使用bash的coproc
comamnd。 (avaliable只有在4.0+) - 它就像KSH的|&
基于答案:
我写了两个shell脚本与我的游戏服务器进行通信。
第一个脚本在计算机启动时运行。它启动服务器,并配置它来读取/接收我的命令,而它在后台运行:
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
# Set up a pipe named `/tmp/srv-input`
rm /tmp/srv-input
mkfifo /tmp/srv-input
# To avoid your server to receive a EOF. At least one process must have
# the fifo opened in writing so your server does not receive a EOF.
cat > /tmp/srv-input &
# The PID of this command is saved in the /tmp/srv-input-cat-pid file
# for latter kill.
#
# To send a EOF to your server, you need to kill the `cat > /tmp/srv-input` process
# which PID has been saved in the `/tmp/srv-input-cat-pid file`.
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`
#
# Replace the `./hlds_run -console -game czero +port 27015` by your application command
./hlds_run -console -game czero +port 27015 > my_logs.txt 2>&1 < /tmp/srv-input &
# Successful execution
exit 0
这第二个脚本,它只是一个包装这让我轻松发送命令到我的服务器:
发送。SH
half_life_folder="/home/jack/Steam/steamapps/common/Half-Life"
half_life_pid_tail_file_name=my_logs_tail_pid.txt
half_life_pid_tail="$(cat $half_life_folder/$half_life_pid_tail_file_name)"
if ps -p $half_life_pid_tail > /dev/null
then
echo "$half_life_pid_tail is running"
else
echo "Starting the tailing..."
tail -2f $half_life_folder/my_logs.txt &
echo $! > $half_life_folder/$half_life_pid_tail_file_name
fi
echo "[email protected]" > /tmp/srv-input
sleep 1
exit 0
现在每次我要发送到我的服务器命令我只是做终端:
./send.sh mp_timelimit 30
该脚本可以让我保持拖尾的过程中,您可将当前终端,因为每次我发送一个命令时,它都会检查是否有尾部进程在后台运行。如果不是,它只是在每次过程发送输出时启动一次,我可以在我用来发送命令的终端上看到它,就像运行附加&
运算符的应用程序一样。
您可以随时打开另一个打开的终端,只听我的服务器服务器控制台。要做到这一点只需使用tail
命令与-f
标志跟随我的服务器控制台输出:
./tail -f /home/user/Half-Life/my_logs.txt
文章是伟大的,但我怎么能发送“的东西somethingelse yetanotherthing”的过程?例如,我有一台服务器运行,它接受“某事”,并对其进行处理。我怎么能这样做? – 2011-05-13 22:22:32