bash 通过 shell 脚本运行多个后台进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20368032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running multiple background processes through shell scripting
提问by user2354302
I'm new to shell scripting, so pardon my lack of knowledge.
我是 shell 脚本的新手,所以请原谅我缺乏知识。
My aim is to run two servers server1
and server2
in the background and then run a python script scriptRun
through my shell script.
我的目标是在后台运行两台服务器server1
,server2
然后scriptRun
通过我的 shell 脚本运行 python脚本。
Step1:
第1步:
Launch server1 (making it run in the background)
Run a few commands on this server (which are customized commands)
启动 server1(使其在后台运行)
在此服务器上运行一些命令(这些是自定义命令)
Step2:
第2步:
- Launch server2
- 启动 server2
Step3:
第三步:
- Run my python script and display its output on the terminal after server1 and server2 are launched
- 运行我的 python 脚本并在 server1 和 server2 启动后在终端上显示其输出
My shell script looks like this:
我的 shell 脚本如下所示:
echo "Launching server1"
java StartServer1.jar && (serverCommand1 && serverCommand2) &
echo "Launching server2"
java StartServer2.jar &&
echo "Running script"
python scriptRun.py
This script does not work at all. I tried removing the serverCommand1 and serverCommand2, this works, but the python script does not wait for server2 to launch.
这个脚本根本不起作用。我尝试删除 serverCommand1 和 serverCommand2,这有效,但 python 脚本不等待 server2 启动。
Also the terminal displays the outputs of server1 and server2, and not the output of the python script.
终端还显示 server1 和 server2 的输出,而不是 python 脚本的输出。
My question is how do I run multiple processes in the background and run another process which is dependent on the previous processes?
我的问题是如何在后台运行多个进程并运行另一个依赖于先前进程的进程?
采纳答案by janos
The &&
in your script seem a bit confused. For the record:
在&&
你的脚本似乎有点混淆。作为记录:
- Put a single
&
after a command to run it in the background &&
is for chaining multiple commands if successful, for examplecmd1 && cmd2
will executecmd1
and only if it exits with success, it will executecmd2
. Both commands will run in the foreground, there is no backgrounding here at all.
&
在命令后放置一个以在后台运行它&&
用于在成功时链接多个命令,例如cmd1 && cmd2
将执行cmd1
并且仅当它成功退出时才会执行cmd2
。这两个命令都将在前台运行,这里根本没有后台。
Maybe you want to do something like this:
也许你想做这样的事情:
echo "Launching server1"
java StartServer1.jar >server1.log 2>server1.err
sleep 5 # give some time for the server to come up
serverCommand1
serverCommand2
echo "Launching server2"
java StartServer2.jar >server2.log 2>server2.err
sleep 5 # give some time for the server to come up
echo "Running script"
python scriptRun.py
Actually, rather than sleeping for a fixed amount of time, it's better if you can detect that the server is ready and react on that. For example in the logs, maybe there is a message indicating that the server is ready, let's say a message that says "READY". Then you can do something like this:
实际上,与其在固定的时间内休眠,不如检测到服务器已准备就绪并对此做出反应。例如在日志中,可能有一条消息表明服务器已准备就绪,假设有一条消息显示“READY”。然后你可以做这样的事情:
echo "Launching server1"
java StartServer1.jar >server1.log 2>server1.err
while :; do sleep 5; grep -q READY server1.log && break; done
That's an infinite loop there, in every sleep it sleeps for 5 seconds and checks if the log contains the text "READY". If it does it ends the loop. You can come up with a variation of this that suits your needs.
这是一个无限循环,在每次睡眠时它会休眠 5 秒并检查日志是否包含文本“READY”。如果它结束循环。您可以提出适合您需求的变体。
回答by devnull
You need to wait
for the background processes to complete before executing your python script. Additionally, redirect STDOUT
and STDERR
from the java processes to /dev/null
if you want to ignore those:
wait
在执行 python 脚本之前,您需要完成后台进程。此外,如果您想忽略这些STDOUT
,请STDERR
从 java 进程重定向/dev/null
到:
echo "Launching server1"
java StartServer1.jar >/dev/null 2>&1 &
echo "Launching server2"
java StartServer2.jar >/dev/null 2>&1 &
wait # wait for the background processes to complete
echo "Running script"
python scriptRun.py
(I'm not sure how your server commands work, but you might need to wait
after starting server1 if you need to issue commands to it.)
(我不确定你的服务器命令是如何工作的,但wait
如果你需要向它发出命令,你可能需要在启动 server1 之后。)