简体   繁体   中英

run Terminal script that executes one command and while that command is running, opens a new tab and runs another command

At the moment I am making a java application. To test it I have to run a server and then a client.

So I want to run this using a bash script:

#!/bin/bash
clear
gradle runServer
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
gradle runClient

Problem : The server when run does not end until you close the game, so the next two commands will not execute . How can I run them concurrently/simultaneously ?

Run the server in the background, then kill it when the script is done.

Here's an example with a simple HTTP server and client.

#!/bin/bash

date > foo.txt
python -m SimpleHTTPServer 1234 &
SERVER_PID="${!}"
# Automatically terminate server on script exit
trap 'kill "${SERVER_PID}"' 0 1 2 3 15

# Wait for server to start
while ! netstat -an -f inet | grep -q '\.1234 '; do
    sleep 0.05
done

# Run client
curl -s http://localhost:1234/foo.txt

Running in another tab gets a lot trickier; this interleaves the output from the client and the server.

$ ./doit.sh
127.0.0.1 - - [19/Sep/2014 23:00:32] "GET /foo.txt HTTP/1.1" 200 -
Fri 19 Sep 2014 23:00:32 MDT

Note the log output from the HTTP server, and the output from the HTTP client. The server is automatically killed afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM