0

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\""

  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.

lets_try
  • 15
  • 1
  • 6
  • I'm not sure you can tweak gnome-terminal from the outside to do this (open tabs). You might want to have a look at terminal multiplexers like `screen` or `tmux` instead. With these you can achieve this (and more) quite easily. They are built for tasks like these. Gnome-terminal is more for GUI users, not for programming it. – Alfe Jan 22 '18 at 10:44
  • The next phase is to run these as background processes. Do you think `screen` or `tmux` would be a better option in that case too ? – lets_try Jan 22 '18 at 10:53
  • `uname -a` doesn't execute remotely because it's executed locally. Syntax is `ssh user@host -c "command"`. – olmstad Jan 22 '18 at 13:15

1 Answers1

2

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 (e. g. 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.

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • `screen` indeed looks good. Is there a command to create windows within a session, split the session and load windows into them ?. Now I am using `Ctrl-A` `c`, `Ctrl-A` `Tab`,`Ctrl-A` `number` to create windows,to toggle split screen and to load windows on the split screen respectively. – lets_try Jan 25 '18 at 09:05
  • @lets_try To start another window from within screen, you can enter `screen -X screen bash`. That will create a new window and start `bash` in it. If you start something else which terminates quickly, e. g. `date`, that will work also but because it is terminating, its window will disappear right after this termination. So you'd have to be lightningly fast to see the window using [C-a] [n]. To address this issue, have a look at https://unix.stackexchange.com/questions/47271/prevent-gnu-screen-from-terminating-session-once-executed-script-ends – Alfe Jan 25 '18 at 11:25