Avoid gnome-terminal close after script execution?

2019-01-03 09:08发布

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"

7条回答
别忘想泡老子
2楼-- · 2019-01-03 09:14

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\""
查看更多
欢心
3楼-- · 2019-01-03 09:19

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

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-03 09:23

Finally this one works for me:

gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"
查看更多
叼着烟拽天下
5楼-- · 2019-01-03 09:27
  • 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.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-03 09:28

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
查看更多
迷人小祖宗
7楼-- · 2019-01-03 09:31

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.

查看更多
登录 后发表回答