Problem
I wrote a bash script to open many gnome terminals, connect to classroom PCs through ssh, and run a script.
How can I prevent the gnome-terminal from closing when the script has completed? It’s worth noting that I’d like to be able to type in more commands on the terminal.
An example of my code is as follows:
gnome-terminal -e "ssh root@<ip> cd /tmp && ls"
Asked by Marten Bauer
Solution #1
As far as I can tell, you want gnome-terminal to open, execute some commands, and then return to the prompt for additional commands. Although Gnome-terminal is not intended for this use, there are workarounds:
$ gnome-terminal -e "bash -c \"echo foo; echo bar; exec bash\""
Because bash -c will terminate once the instructions are completed, the exec bash at the end is required. If you don’t use exec, the current process will be replaced by the new one, and you’ll have two bash processes running.
Prepare somercfile:
source ~/.bashrc
echo foo
echo bar
Then run:
$ gnome-terminal -e "bash --rcfile somercfile"
Prepare scripttobash:
#!/bin/sh
echo foo
echo bar
exec bash
Set this file’s permissions to executable.
Then run:
$ gnome-terminal -e "./scripttobash"
You may also construct a genericscripttobash:
#!/bin/sh
for command in "$@"; do
$command
done
exec bash
Then run:
$ gnome-terminal -e "./genericscripttobash \"echo foo\" \"echo bar\""
Every method has its own peculiarities. You must make a decision, but make it intelligently. The first option appeals to me because of its verbosity and simplicity.
Having said that, you might find the following useful: http://www.linux.com/archive/feature/151340
Answered by Lesmana
Solution #2
Finally, here’s one that I like:
gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"
Answered by Lukasz Frankowski
Solution #3
Answered by Gilles ‘SO- stop being evil’
Solution #4
To make terminal close bash process when you close your terminal gui, use -ic instead of -i:
gnome-terminal -e "bash -ic \"echo foo; echo bar; exec bash\""
Answered by Zini
Solution #5
The optimal solution would be to utilize echo “Press any key” to request user input.
However, it does not appear to work if you double-click in Nautis or Nemo and select execute in a terminal.
In the case of Ubuntu, a shell named dash is utilized, which is designed for quick start-up and execution with only standard capabilities. As a result, the shebang is the first line to utilize in order to make proper use of bash features. Normally, #!/bin/bash or something similar would be used. This should be #!/usr/bin/env bash, according to Ubuntu.
Many workarounds exist to keep hold of the screen before the interpreter sees a syntax error in a bash command.
The Ubuntu solution that worked for me was:
#!/usr/bin/env bash
your code
echo Press a key...
read -n1
Answered by Leo
Post is based on https://stackoverflow.com/questions/3512055/avoid-gnome-terminal-close-after-script-execution