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"
share|improve this question
    
possible duplicate of Prevent Gnome Terminal From Exiting After Execution – givanse Feb 4 '14 at 19:05
    
Also: askubuntu.com/q/3359/6506 – givanse Feb 4 '14 at 19:05

As I understand you want gnome-terminal to open, have it execute some commands, and then drop to the prompt so you can enter some more commands. Gnome-terminal is not designed for this use case, but there are workarounds:

Let gnome-terminal run bash and tell bash to run your commands and then run bash

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

The exec bash at the end is necessary because bash -c will terminate once the commands are done. exec causes the running process to be replaced by the new process, otherwise you will have two bash processes running.

Let gnome-terminal run bash with a prepared rcfile which runs your commands

Prepare somercfile:

source ~/.bashrc
echo foo
echo bar

Then run:

$ gnome-terminal -e "bash --rcfile somercfile"

Let gnome-terminal run a script which runs your commands and then drops to bash

Prepare scripttobash:

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

Set this file as executable.

Then run:

$ gnome-terminal -e "./scripttobash"

Alternatively you can make 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 it's quirks. You must choose, but choose wisely. I like the first solution for its verbosity and the straightforwardness.

All that said, this might be of good use for you: http://www.linux.com/archive/feature/151340

share|improve this answer
    
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
  • 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.

share|improve this answer
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@<ip> 2. cd /root/install && ./WriteImage.sh && InitImage.py 3. <some other commands by hand> 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 8:24
    
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 6 '15 at 3:19
    
@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 Super User or Unix & Linux rather than Stack Overflow since it isn't a programming problem (but probably on Stack Overflow too anyway). – Gilles Oct 6 '15 at 9:56

Finally this one works for me:

gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"
share|improve this answer

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\""
share|improve this answer
    
Thanks, this also works for mate-terminal. – GrandAdmiral Mar 6 '15 at 22:07

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
share|improve this answer

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.

share|improve this answer

Use nohup command.

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

Hope this will help you.

share|improve this answer
    
The window still closes immediately. – bstpierre Aug 19 '10 at 1:19
    
Probably you should use nohup to launch main script? – sourcerebels Aug 19 '10 at 4:49
1  
I can't see how nohup will help here. – bstpierre Aug 19 '10 at 18:02
    
nohup does the opposite of what the OP is asking for. – Alex Mar 31 '17 at 18:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.