Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid gnome-terminal close after script execution?

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" 
like image 434
Marten Bauer Avatar asked Aug 18 '10 12:08

Marten Bauer


People also ask

How do I hold GNOME Terminal?

In gnome-terminal , go to preferences, the "Title and command" tab. Then click the drop-down list "When command finishes", choose "Keep terminal open".

How do I change GNOME Terminal settings?

To configure GNOME terminal, go to Edit > Preferences. From here, you can configure some global and profile specific settings of GNOME Terminal.

How do I stop a bash script from closing?

To terminate the script in case of an error, we can use the “-e” option. and the script exits on an error, it closes the terminal window.


1 Answers

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

like image 73
Lesmana Avatar answered Sep 18 '22 14:09

Lesmana