views:

288

answers:

4

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?

Here is an example of my code:

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

thx for your help

A: 

Use nohup command.

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

Hope this will help you.

SourceRebels
The window still closes immediately.
bstpierre
Probably you should use nohup to launch main script?
SourceRebels
I can't see how nohup will help here.
bstpierre
+1  A: 
  • 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.

Gilles
Marten Bauer
+1  A: 

if i understand correctly you want to have a gnome-terminal open, have it execute some commands, and then drop to the prompt so you can enter some more commands.

it seems to me that gnome-terminal is not designed for this use case, but there are workarounds:

let gnome-terminal run your commands followed by bash

$ gnome-terminal -e "echo foo; echo bar; bash"

strangely this does not work on my system. it will print foo; echo bar; bash. i tried some escaping of the semicolons, but i did not manage to get it working the way i wanted. might be a "feature" of gnome-terminal.

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 file as executable

then run

$ gnome-terminal -e "./scripttobash"

or 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 it's verbosity and the straightforwardness, if you manage to get it working.

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

lesmana
A: 

none of these work I get the following Please: command not found

wlyon