70

I created a bash script that opens several gnome-terminals, connect to classroom computers via ssh and run a script.

How can I avoid that the gnome-terminal closes after the script is finished? Note that I also want to be able to enter further commands in the terminal.

Here is an example of my code:

gnome-terminal -e "ssh root@<ip> cd /tmp && ls"
Lesmana
  • 25,663
  • 9
  • 82
  • 87
Marten Bauer
  • 3,099
  • 5
  • 22
  • 18
  • possible duplicate of [Prevent Gnome Terminal From Exiting After Execution](http://stackoverflow.com/questions/4465930/prevent-gnome-terminal-from-exiting-after-execution) – givanse Feb 04 '14 at 19:05
  • Also: http://askubuntu.com/q/3359/6506 – givanse Feb 04 '14 at 19:05

10 Answers10

97

Let gnome-terminal run bash and tell bash to run your commands and then start a new bash:

$ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"

explanation:

gnome terminal runs bash ...

$ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
                    ^^^^

which runs your commands ...

$ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
                             ^^^^^^^^  ^^^^^^^^

and then reexecutes bash.

$ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
                                                 ^^^^^^^^^

gnome terminal will not close if something is still running. in this case the second bash is still running. this makes gnome terminal not close and you can interact with bash inside gnome terminal as normal.

exec is not mandatory here but has some implication which are beneficial for this use case.

if the commands are many or complex you can put them in a script:

$ gnome-terminal -- bash -c "./scripttorun; exec bash"

you can also reexecute bash in the script directly

Prepare scripttobash:

#!/bin/sh
echo foo
echo bar
exec bash

Then run:

$ gnome-terminal -- ./scripttobash

the advantage is the gnome terminal command became quite simple.

the disadvantage is that the script now always runs a second bash. which means you cannot run the script independently. well maybe you can but the second bash might cause trouble or confusion.


it seems that the bash --rcfile can be used for this

Prepare somercfile:

source ~/.bashrc
echo foo
echo bar

Then run:

$ gnome-terminal -- bash --rcfile somercfile

bash will stay open after running somercfile.

i must admit i do not understand completely why --rcfile has this behaviour but it does.


for completeness

there is an option to keep gnome terminal open after executing the command. but you will not be able to interact anymore. just read the output.

  1. go to preferences (hamburger button -> preferences)
  2. go to profiles (i recommend to create a new profile for this case)
  3. go to command tab
  4. set "when command exits" to "hold the terminal open"

if you created a new profile you can use it like this:

gnome-terminal --profile=holdopen -- ./scripttorun

Every method has it's quirks. You must choose, but choose wisely.

I like the first solution. it does not need extra files or profiles. and the command says what it does: run commands then run bash again.

All that said, since you used ssh in your example, you might want to take a look at pssh (parallel ssh). here an article: https://www.cyberciti.biz/cloud-computing/how-to-use-pssh-parallel-ssh-program-on-linux-unix/

Lesmana
  • 25,663
  • 9
  • 82
  • 87
  • It might be worth noting that the `rcfile` solution allows commands to be run _after_ the ` .bashrc` file is sourced, while none of the other options allow you to do that. – M. Andres Oct 29 '16 at 15:36
  • Although this is crazy after the fact I am glad I stumbled across this post. I have a script which used the "Hold" method in the profile, and idiots keep clicking the relaunch button that pops up and causing issues! – Kip K Feb 10 '17 at 14:04
18

Finally this one works for me:

gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"
Lukasz Frankowski
  • 2,955
  • 1
  • 31
  • 32
11
  • Stack Overflow answer: the terminal closes when the command run inside it has finished, so you need to write a command that doesn't terminate immediately. For example, to leave the terminal window open until you press Enter in it:

    gnome-terminal -e "ssh host 'cd /tmp && ls'; read line"
    
  • Super User answer: Create a profile in which the preference “Title and Command/When command exits” is set to “Hold the terminal open”. Invoke gnome-terminal with the --window-with-profile or --tab-with-profile option to specify the terminal name.

Community
  • 1
  • 1
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • 1
    This idea helps but doesn't do what I want. I will do some commands in the terminal automatically as described above. After the execution I will be able to enter some commands by hand. In the past I did by hand (copy/paste): 1. ssh root@ 2. cd /root/install && ./WriteImage.sh && InitImage.py 3. The line 2. I copied by hand to 20 terminals for 20 computers. Can't I do step 1. and 2. automatically and also be able to enter other commands by hand as in step 3.? – Marten Bauer Aug 19 '10 at 08:24
  • 2
    The problem with `--window-with-profile' (which opens separate terminals) or `--tab-with-profile` (which opens tabs in a single terminal) is that once the script finishes running in the tab/window, or when I press `Ctrl+c`, the tab/window closes immediately. I don't want it to close. I want the tab to remain open and to show the bash prompt so that I can continue typing commands in the bash prompt of that tab. Isn't that possible in any way? `read line` only pauses for a user input, and that is not what I'm looking for. Any help would be appreciated. – Nav Oct 06 '15 at 03:19
  • 1
    @Nav That's a different question: you want to leave the *shell* open, not the terminal. I'm sure it's been asked before, possibly on [su] or [unix.se] rather than [so] since it isn't a programming problem (but probably on [so] too anyway). – Gilles 'SO- stop being evil' Oct 06 '15 at 09:56
5

Run with -ic instead -i to make terminal close bash proccess when you close your terminal gui:

gnome-terminal -e "bash -ic \"echo foo; echo bar; exec bash\""
Zini
  • 909
  • 7
  • 15
5

As of January 2020, the -e option in gnome-terminal still runs properly but throws out the following warning:

For -e:

# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

# Use “-- ” to terminate the options and put the command line to execute after it.

Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:

$ gnome-terminal -- "./scripttobash"
$ gnome-terminal -- "./genericscripttobash \"echo foo\" \"echo bar\""

I hope this helps anyone else presently having this issue :)

Member2017
  • 431
  • 2
  • 8
  • 16
3

The ideal solution would be to ask for a user input with echo "Press any key".

But if double-click in Nautis or Nemo and select run in a terminal, it doesn't seem to work.

In case of Ubuntu a shell designed for fast start-up and execution with only standard features is used, named dash I believe. Because of this the shebang is the very first line to start with to enable proper use of bash features. Normally this would be: #!/bin/bash or similar. In Ubuntu I learned this should be: #!/usr/bin/env bash.

Many workarounds exist to keep hold of the screen before the interpreter sees a syntax error in a bash command.

The solution in Ubuntu that worked for me:

#!/usr/bin/env bash

your code

echo Press a key...
read -n1
Leo
  • 178
  • 1
  • 9
0

For a solution applicable to any terminal, there is a script that opens a terminal, runs the command specified and gives you back the prompt in that new terminal:

https://stackoverflow.com/a/60732147/1272994

philipper
  • 373
  • 3
  • 11
0

I really like the bash --rcfile method

I just source ~/.bashrc then add the commands I want to the new startrc.sh

now my automated start.sh work environment is complete... for now

Liz
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 17 '21 at 07:17
-3

If running a bash script just add gedit afile to the end of the script and that will hold gnome-terminal open. "afile" could be a build log which it was in my case.

Did not try just using gedit alone but, that would properly work too.

NickyP
  • 91
  • 1
  • 7
-5

Use nohup command.

nohup gnome-terminal -e "ssh root@ cd /tmp && ls"

Hope this will help you.

sourcerebels
  • 5,140
  • 1
  • 32
  • 52