Keep the gnome terminal open after the program is finished - terminal

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.

Related

split terminal into different tabs

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

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

open gnome terminal tabs programmatically and execute commands in sequence

When working remotely, I have a series of tabs that I open in gnome-terminal, and commands that I execute in them. I would like to automate all this setup as a single command.
If these commands could run independently and in parallel, I'd just adapt the answer to this question. In fact, I tried, using the following shell script:
gnome-terminal --working-directory="/home/superelectric" --tab -t "gate" -e 'bash -c "export BASH_POST_RC=\"ssh gate_tunnel\"; exec bash"' --tab -t "mydesktop" -e 'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'
Spread out over multiple lines, for readability:
gnome-terminal \
--working-directory="/home/superelectric" \
--tab \
-t "gate" \
-e \
'bash -c "export BASH_POST_RC=\"ssh gate_tunnel\"; exec bash"' \
--tab \
-t "mydesktop" \
-e \
'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'
The first part opens a tab, names it 'gate', and executes 'ssh gate_tunnel' within it. This is an ssh alias that opens a tunnel to 'mydesktop' at school, through the school's outward-facing server, 'gate'.
The second part opens another tab, names it 'mydesktop', and executes 'ssh tunneled_mydesktop' within it. This is another ssh alias, which connects to mydesktop through the tunnel.
~/.ssh/config:
Host gate_tunnel
LocalForward 8023 <my_desktop_at_school>:22
HostName <my_school_server>
That's the theory. In practice, the two commands execute in parallel, whereas I need to ensure that the first tab's command (open tunnel) completes before executing the second tab's command (connect through tunnel).
Is there maybe some command I can execute in the second tab, that 'waits' until the ssh tunnel is opened?
Ok, I think i get it. As i mentioned in the comments the first thing that comes to mind for reaching your school desktop from the outside is to ssh into the school gate and from there ssh into your desktop with something like:
$ ssh -t gate.school.edu ssh desktop_name
There's only one tab then, so your problem doesn't exist.
However there's something very cool with your current setup:
From home it's almost as if you had a direct connection to your desktop machine, so you can scp into it directly and forget about gate. With the solution above that's not possible anymore because we end up with an indirect connection: If you want to scp you have to do it from gate and that sucks.
Check out this article on using ssh's ProxyCommand feature:
Transparent Multi-hop SSH
You get the best of both worlds then :)
Hmm... this may not be a perfect solution. Ideally you should use something that monitors the ssh connection. But, you can check the ssh process with ps. And wait for ssh command to come alive.
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do # try 10 times
if ps aux ¦ grep <my_desktop_at_school> then
# the tunnel connected now execute the second command
'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'
else
continue # or you could do something here if you wish
fi
sleep 10 # sleep for 10 seconds and try again
let COUNTER=COUNTER+1
done
You will have to run this script in the second tab.
Hope it helps.

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