避免在脚本执行后关闭gnome-terminal ?

时间:2022-05-23 06:54:34

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

我创建了一个bash脚本,该脚本打开几个gnome-terminal,通过ssh连接到教室计算机并运行脚本。

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.

我如何避免在脚本完成后,gnome-terminal关闭?注意,我还希望能够在终端中输入更多的命令。

Here is an example of my code:

下面是我的代码示例:

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

7 个解决方案

#1


78  

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:

正如我所理解的,您希望gnome-terminal打开,让它执行一些命令,然后切换到提示符,以便您可以输入更多的命令。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.

最后的exec bash是必要的,因为一旦命令完成,bash -c将终止。exec使正在运行的进程被新进程替换,否则您将有两个正在运行的bash进程。

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

Prepare somercfile:

准备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:

准备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:

或者您可以创建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

总之,这可能对您有很好的用处:http://www.linux.com/archive/feature/151340

#2


11  

Finally this one works for me:

最后这一个对我有用:

gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"

#3


10  

  • 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:

    堆栈溢出回答:当内部的命令运行完成时,终端关闭,因此您需要编写一个不立即终止的命令。例如,要让终端窗口一直打开,直到按下Enter键:

    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.

    超级用户回答:创建一个配置文件的偏好”称号和命令/当命令退出”设置为“终端开放”。调用gnome终端——window-with-profile或tab-with-profile选项指定终端的名字。

#4


4  

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

使用-ic代替-i,在关闭终端gui时,让终端关闭bash进程:

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

#5


0  

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

理想的解决方案是请求用户输入echo“按任意键”。

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

但如果在Nautis或Nemo中双击并选择在终端中运行,这似乎是行不通的。

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.

在Ubuntu的情况下,一个专为快速启动和执行而设计的shell只使用了标准特性,我相信叫做dash。正因为如此,shebang是开始时允许正确使用bash特性的第一行。通常这是:#!/bin/bash或类似。在Ubuntu中,我知道这应该是:#!/usr/bin/env bash。

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

在解释器看到bash命令中的语法错误之前,存在许多替代方法来保持对屏幕的控制。

The solution in Ubuntu that worked for me:

Ubuntu的解决方案对我起了作用:

#!/usr/bin/env bash

your code

echo Press a key...
read -n1

#6


-2  

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.

如果运行一个bash脚本,只需将gedit afile添加到脚本的末尾,并保持gnome-terminal打开。“afile”可以是一个构建日志,在我的例子中是这样的。

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

没有尝试只使用gedit,但是,这也可以正常工作。

#7


-5  

Use nohup command.

使用nohup命令。

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

Hope this will help you.

希望这能对你有所帮助。

#1


78  

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:

正如我所理解的,您希望gnome-terminal打开,让它执行一些命令,然后切换到提示符,以便您可以输入更多的命令。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.

最后的exec bash是必要的,因为一旦命令完成,bash -c将终止。exec使正在运行的进程被新进程替换,否则您将有两个正在运行的bash进程。

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

Prepare somercfile:

准备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:

准备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:

或者您可以创建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

总之,这可能对您有很好的用处:http://www.linux.com/archive/feature/151340

#2


11  

Finally this one works for me:

最后这一个对我有用:

gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"

#3


10  

  • 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:

    堆栈溢出回答:当内部的命令运行完成时,终端关闭,因此您需要编写一个不立即终止的命令。例如,要让终端窗口一直打开,直到按下Enter键:

    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.

    超级用户回答:创建一个配置文件的偏好”称号和命令/当命令退出”设置为“终端开放”。调用gnome终端——window-with-profile或tab-with-profile选项指定终端的名字。

#4


4  

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

使用-ic代替-i,在关闭终端gui时,让终端关闭bash进程:

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

#5


0  

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

理想的解决方案是请求用户输入echo“按任意键”。

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

但如果在Nautis或Nemo中双击并选择在终端中运行,这似乎是行不通的。

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.

在Ubuntu的情况下,一个专为快速启动和执行而设计的shell只使用了标准特性,我相信叫做dash。正因为如此,shebang是开始时允许正确使用bash特性的第一行。通常这是:#!/bin/bash或类似。在Ubuntu中,我知道这应该是:#!/usr/bin/env bash。

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

在解释器看到bash命令中的语法错误之前,存在许多替代方法来保持对屏幕的控制。

The solution in Ubuntu that worked for me:

Ubuntu的解决方案对我起了作用:

#!/usr/bin/env bash

your code

echo Press a key...
read -n1

#6


-2  

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.

如果运行一个bash脚本,只需将gedit afile添加到脚本的末尾,并保持gnome-terminal打开。“afile”可以是一个构建日志,在我的例子中是这样的。

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

没有尝试只使用gedit,但是,这也可以正常工作。

#7


-5  

Use nohup command.

使用nohup命令。

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

Hope this will help you.

希望这能对你有所帮助。