split terminal into different tabs - bash

I'm using this script shell to run multiple commands, my problem is that each result of the command appears in a different window, my goal is to have one window with different tabs.
here is my code:
#!/bin/bash
echo "java -jar SBM.jar" >/tmp/ma_commande
gnome-terminal -e 'bash -c ". /tmp/ma_commande;read" ' -t "Building Manager"
rm /tmp/ma_commande
echo "java -jar UserAgent.jar" >/tmp/ma_commande
gnome-terminal -e 'bash -c ". /tmp/ma_commande;read" ' -t "User Agent"
rm /tmp/ma_commande
echo "java -jar PSM.jar" >/tmp/ma_commande
gnome-terminal -e 'bash -c ". /tmp/ma_commande;read"' -t "PSM"
rm /tmp/ma_commande

you can use these apps to split terminal window as you wish on Linux or Unix:
byobu: http://byobu.co/
screen: https://www.gnu.org/software/screen/
these are window manager and the more modern Tmux terminal multiplexer, and works on most Linux, BSD, and Mac distributions

Related

Shell Script - SSH using Screen

I am trying to develop a shell script / bash script with ssh and screen, using Ubuntu / Linux.
Could you help me to correct what is wrong here?
#!/bin/bash
ssh -i keypair.pem -t ubuntu#ec2-address "screen -S teste"; cd 'home'; cd 'Shell'; ./'AWS - teste.sh' "
It doesn't work.
You fell victim to shell quoting - which is tricky when using ssh. Your command does something alike
cd home/Shell; ./AWS - teste.sh
so I would try
ssh -i keypair.pem -t ubuntu#ec2-address screen -S teste "sh -c \"cd home/Shell; ./AWS - teste.sh\""

Gnome Terminal: How to open up multiple tabs, execute commands and return to zsh/bash

I'm trying to automate my morning start up process. Often I'll start multiple running scripts that I will exit (using ctrl+c) and restart manually as needed.
So I'm looking to create a bash script that
Starts gnome-terminal
Opens some tabs and executes a number of
commands
Returns back to zsh upon exit or completion of the script so I can
manually enter more commands
Currently I have,
#!/bin/bash
gnome-terminal \
--tab -t "Server" -e "bash -ic \"cd ~/Dev/server; npm start; exec zsh\"" \
--tab -t "Framework" -e "bash -ic \"cd ~/Dev/framework; npm start; exec zsh\"" \
--tab -t "Client" -e "bash -ic \"cd ~/Dev/client; npm start; exec zsh\"" \
--tab -t "Admin" -e "bash -ic \"cd ~/Dev/admin;npm start; exec zsh\""
The problem with this solution is that tabs may or may not jump back into zsh. Sometimes 3 tabs will, sometimes one. Ideally I'd like all 4 to go back into zsh.
If anyone could help me around this, I'd be grateful.
You can take gnome-terminal out of the equation and it still won't work:
bash -ic "npm start; bash"
Press Control-C, and you won't get a shell. For bash, we can get around this problem using the --rcfile option:
bash --rcfile <(echo "npm start")
This way we don't read the real .bashrc, so since we probably want to read that, let's modify this a bit:
bash --rcfile <(echo ". ~/.bashrc; npm start")
For zsh on the other hand it doesn't work with the equivalent(?) --rcs option, but it does when started with the ZDOTDIR variable. So we create a .zshrc for each of ~/Dev/server, ~/Dev/framework, ~/Dev/client, ~/Dev/admin with the following content:
First:
. ~/.zshrc
cd ~/Dev/server
npm start
Second:
. ~/.zshrc
cd `~/Dev/framework`
npm start
etc.
Your gnome-terminal command will look like this:
gnome-terminal \
--tab "Server" -e "sh -c 'ZDOTDIR=/path/to/directory/containing/first/.zshrc'" \
--tab "Framework" -e "sh -c 'ZDOTDIR=/path/to/directory/containing/second/.zshrc'"
# etc

Keep the gnome terminal open after the program is finished

This script opens a gnome terminal and 4 tabs in it , but once the program finishes the tabs gets closed so i can't see the output. It doesn't happen when i run each program manually. How can keep the tab open , even when the program is finished .
gnome-terminal --tab -e "optirun yarpserver" \
--tab -e "sh -c 'sleep 20 ; optirun iCub_SIM'" \
--tab -e "sh -c 'sleep 86 ; optirun simCartesianControl'" \
--tab -e "sh -c 'sleep 116 ; optirun iKinCartesianSolver --context simCartesianControl/conf --part left_arm'" \
Not sure how to do it on the command line (man gnome-terminal doesn't seem to indicate a specific option for that, but you can start a gnome-terminal, set specific options (one of which would be "When command exits: Hold the terminal open"), and save your settings as a specific profile. There is a command-line option for selecting a specific profile to use, so that should accomplish what you want.

Open a program in new terminal tab using shell script

This is a small shell script i wrote .
x-terminal-emulator -e "optirun yarpserver" &
sleep 6
x-terminal-emulator -e "optirun iCub_SIM" &
sleep 60
x-terminal-emulator -e "optirun simCartesianControl" &
sleep 30
x-terminal-emulator -e "optirun iKinCartesianSolver --context simCartesianControl/conf --part left_arm" &
What this does is , opens a new terminal for each program . What i want to do is open new terminal tab instead of a terminal . How should i go about doing this ?
This thread is really old but if someone comes here, I leave a bash script that I created to launch multiple tabs to run different commands:
#!/bin/bash
# Array of commands to run in different tabs
commands=(
'tail -f /var/log/apache2/access.log'
'tail -f /var/log/apache2/error.log'
'tail -f /usr/local/var/postgres/server.log'
)
# Build final command with all the tabs to launch
set finalCommand=""
for (( i = 0; i < ${#commands[#]}; i++ )); do
export finalCommand+="--tab -e 'bash -c \"${commands[$i]}\"' "
done
# Run the final command
eval "gnome-terminal "$finalCommand
Just add the commands in the array and execute.
Source: http://joaoperibeiro.com/command-line-script-to-launch-multiple-tabs/
I think your best option is to use tmux to do the job. Here just a quick example and a step by step explanation to it. Here I use only vertical splits which may be confusing you should read into the tmux manpage to see how to select-panes.
First create a new tmux session in detached mode
Then send the appropriate command to launch your first program
Create a new vertical Split
Send appropriate command to launch your second program
and so on ...
tmux new-session -d -s foo
tmux send-keys -t foo 'optirun yarpserver' Enter
tmux split-window -v -t foo
tmux send-keys -t foo 'optirun iCub_SIM' Enter
tmux split-window -v -t foo
tmux send-keys -t foo 'optirun simCartesianControl' Enter
tmux split-window -v -t foo
tmux send-keys -t foo 'optirun iKinCartesianSolver --context simCartesianControl/conf --part left_arm' Enter
Hope this helps you.

How to create a new terminal session and execute multiple commands

I'm looking for a way to automate the start-up of my development environment. I have three virtual machines that have to be started, then have to ssh to each of them and open VPN on them.
So far I've gotten them to start and managed to ssh to them:
#!/bin/sh
virsh start virtual_1
virsh start virtual_2
virsh start virtual_3
sleep 2m
gnome-terminal --title "virtual_3: server" -x ssh root#192.168.1.132 &
gnome-terminal --title "virtual_2: 11.100" -x ssh root#192.168.11.100 &
gnome-terminal --title "virtual_1: 12.100" -x ssh root#192.168.12.100 &
How do I execute an additional command in each of the terminals which starts openvpn?
For simplicity I'm trying to echo 1 in each terminal instead of starting VPN.
I've found that multiple commands on terminal start can be run like:
gnome-terminal -x bash -c "cmd1; cmd2"
So for one terminal to keep it simple I changed:
gnome-terminal --title "virtual_3: server" -x ssh root#192.168.1.132 &
to:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh root#192.168.1.132 ; echo 1" &
But 1 wasn't printed in the terminal of virtual_3.
Then I thought, maybe the command is being executed too quickly, before the terminal is ready, so I tried adding &&:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh root#192.168.1.132 &&; echo 1" &
But that gave no result either.
First of all, if you run
gnome-terminal -x bash -c "cmd1; cmd2"
you get bash to execute cmd1 and cmd2. It doesn't first execute cmd1 and then give cmd2 to its result. ssh is a program run in the terminal and your cmd2 won't be executed until that is finished.
So you need to run ssh and tell that to execute your command.
You can do so by:
ssh user#address "command_to_execute"
However, ssh exits after the command is finished. As you can see in "With ssh, how can you run a command on the remote machine without exiting?", you can execute ssh with the -t option so it doesn't quit:
ssh -t user#address "command_to_execute"
So your command in the end becomes:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh -t root#192.168.1.132 'echo 1'"
You are right, giving -t alone is not enough (although necessary). -t allocates buffer for the tty but doesn't execute bash for you. From the manual of ssh:
-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
If command is specified, it is executed on the remote host instead of a login shell.
So what you need is, to execute bash your self. Therefore:
ssh -t user#address "command_to_execute; bash"

Resources