开发者

Start serval applications and pipe it to current terminal

How can I start serval applications in one script and pipe the output to current terminal?

For development I need a script which starts three webservers (on three ports, of course) and pipe the output of these to curren开发者_开发百科t terminal.

The difficult is to stop these webservers at the end of shell. I have to send a signal or better send [Ctrl + C] to all webservers.

I hope you're understanding me ;-)


EDIT

#!/bin/bash
netstat &
p1=$!
netstat &
p2=$!
netstat &
p3=$!
trap "kill $p1 $p2 $p3" SIGINT
wait $p1 $p2 $p3

Script starts the other processes in the background and remembers their process IDs. It then waits for all of them to terminate. In the meantime, upon receiving a SIGINT (i.e. a ^C), it will kill those processes, which will then terminate the script too (for lack of more stuff to do). So by ^C'ing your script, you can kill all the background server processes.

Oh yeah... I'm using netstat as a long-running test command. Substitute whatever you want to run.

Finally, you can choose which signal to send your processes with kill. Check man kill for options; I think INT (the equivalent of ^C) is the default.


web_server_1 &
ws1=$!
web_server_2 &
ws2=$!
web_server_3 &
ws3=$!
# Kill some time
kill -s SIGINT $ws1 $ws2 $ws3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜