简体   繁体   中英

bash: in 1 script file execute multiple commands simultaneously in different terminals

I have a bash script file which main purpose is to (cross) compile binaries for several platforms. What I want is simultaneously execute multiple commands (so start at the same time) in different terminals. To be clear they must be started (and closed) from the same (bash) .sh file.

I tried (just as test from the command prompt): gnome-terminal --title "terminal 1" -x bash -c "ssh -t root@192.xyz 'echo 1'"

but did not work; error msg: "Failed to parse arguments: Cannot open display:" Of course gnome-terminal is installed.

Actually what I want in the (bash) sh file is:

(run in main terminal): command A; command B; command C

at the same time: (run in terminal 2): command D, command E; command F (then close the terminal)

at the same time (run in terminal 3): command G, command H; command I (then close the terminal)

when terminal 2 and 3 are finished and closed: execute rest of the .sh file

You can utilize background subshells and then a wait :

# A, B, C

(
    # D, E, F
) &

( 
    # G, H, I
) &

wait

Note that this sends all output to the main script's stdout . If you want to save the output of the subshells separately, use (stuff) > file.log & or (stuff) | tee file.log & (stuff) | tee file.log & for each.

Your command is fine. The reason why it says "Failed to parse arguments: Cannot open display:" is because DISPLAY isn't set, so gnome-terminal doesn't know where to open a window. Are you perhaps trying to run the script from ssh, or from a GNU screen session started outside of X?

If you're running from ssh, you can use ssh -X to open the terminals on the client you ssh from. If you run in screen and/or want to open gnome-terminal on the first display on the box, export DISPLAY=:0 in the beginning of your script.

If you want to open it on another display on the server (eg a NX or VNC session), open a terminal manually there, echo $DISPLAY to see which display number it is, and then set DISPLAY to that value in your script.

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