打开新的 gnome-terminal 并运行命令 [英] Open new gnome-terminal and run command

查看:94
本文介绍了打开新的 gnome-terminal 并运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本来打开一个新终端,然后从该终端运行一个单独的 python 脚本.

I'm trying to write a script that opens a new terminal then runs a separate python script from that terminal.

我试过了:

os.system("gnome-terminal 'python f.py'")

p = Popen("/usr/bin/gnome-terminal", stdin=PIPE)
p.communicate("python f.py")

但两种方法都只打开一个新终端,不运行 f.py.我将如何打开终端并运行单独的脚本?

but both methods only open a new terminal and do not run f.py. How would I go about opening the terminal AND running a separate script?

我想打开一个新的终端窗口,因为 f.py 是一个运行 serve_forever() 的简单服务器.我希望原始终端窗口保持自由"以运行其他命令.

I would like to open a new terminal window because f.py is a simply server that is running serve_forever(). I'd like the original terminal window to stay "free" to run other commands.

推荐答案

像大多数终端一样,gnome 终端需要 options 来执行命令:

Like most terminals, gnome terminal needs options to execute commands:

gnome-terminal [-e, --command=STRING] [-x, --execute]

您可能需要添加 -x 选项:

You probably need to add -x option:

x, --execute

x, --execute

在终端内执行命令行的其余部分.

Execute the remainder of the command line inside the terminal.

所以:

os.system("gnome-terminal -x python f.py")

除非您将 & 添加到命令行 BTW,否则不会在后台运行您的进程.

That would not run your process in the background unless you add & to your command line BTW.

communicate 尝试需要为您的输入换行,但也应该可以工作,但是像终端这样的复杂过程不会喜欢"被重定向.这似乎是向后使用交互式工具.同样,这将阻塞直到终止.可以使用p.stdin.write("python f.py\n")来控制python脚本.但在这种情况下,它不太可能奏效.

The communicate attempt would need a newline for your input but should work too, but complex processes like terminals don't "like" being redirected. It seems like using an interactive tool backwards. And again, that would block until termination. What could work would be to use p.stdin.write("python f.py\n") to give control to the python script. But in that case it's unlikely to work.

所以看起来你甚至不需要python来做你想做的事.你只需要运行

So it seems that you don't even need python do to what you want. You just need to run

python f.py &

在外壳中.

这篇关于打开新的 gnome-terminal 并运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆