简体   繁体   中英

Multiple tabs and Bash scripting

I am trying to open multiple tabs and execute a series of commands in each tab. Lets say I open 3 tabs tab1, tab2, tab3. Then in each tab I would like to execute following:

  • ssh user@address (PublicKey Authentication is setup and hence no need to enter password)

  • Launch python scripts (python some.py)

  • Hold the tab open after executing the commands to see the outputs.

I went through some threads and have a rough outline for Bash script.

#!/bin/bash

echo "Script running"
gnome-terminal -e "bash -c \"ssh user@address; uname -a; exec bash\""

When I run the above script, a new terminal opens and I can see that I have ssh-ed into the target address but the other command uname -a didnot execute.

I would like to build upon this to implement the following:

  1. Open multiple tabs and run commands. Ex : gnome-terminal --tab -e "bash -c \\"ssh user@address; python file1.py; exec bash\\"" -tab -e "bash -c \\"ssh user@address; python file2.py; exec bash\\"" gnome-terminal --tab -e "bash -c \\"ssh user@address; python file1.py; exec bash\\"" -tab -e "bash -c \\"ssh user@address; python file2.py; exec bash\\""

  2. Wait for one of the python file to start executing before opening another tab and repeating the process for another python file.

Also is there a better way to implement the same task ?

The above code snippet was from this thread .

You should consider using screen or tmux or a similar terminal multiplexer for this.

Example usage:

screen -d -m bash -c 'ls; bash'

to initiate a screen session in which ls was executed and then a shell started, and then

screen -X screen bash -c 'date; bash'

to create a new window in the existing screen session, run date therein and then start a shell in that window.

Mind that the programs are run without you seeing their output right away on your controlling terminal. You can then attach to the screen session using

screen -x

Which attach you to the running session and will show you one of the screen windows (the virtual terminals of your two running programs). Typing Ctrl-A n will switch through the windows, Ctrl-A d will detach you again, leaving the programs running, so you can attach later with screen -x .

You can attach from several locations (eg from two different Gnome-terminals) to the same running windows. Both will then show the same contents.

Another advantage of using screen is that you can log out and the programs keep running. If you later login again, you can still attach to the running sessions.

Only a direct attack like a reboot, a kill-signal or an interaction (like pressing Ctrl-C while being attached) will terminate your programs then.

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