打开终端,运行python脚本并保持打开状态以获取结果? [英] Open terminal, run python script and keep it open for results?
问题描述
如何获取用于启动新终端的sh脚本,执行python脚本并使其保持运行状态?python脚本应该在永久循环中连续运行,并在弹出时吐出结果.每当尝试使用gnome-terminal的sh-script时,都会得到:子进程正常退出,状态为2
How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
手动将只是:python home/ubuntu/pyscript.py
Manually it would just be: python home/ubuntu/pyscript.py
有人可以提出一个想法吗?
Could someone give an idea how to do this?
我有要运行的脚本列表,因此诉诸于手动解决方案很繁琐.
I have a list of scripts to run, so resorting to the manual solution is tedious.
推荐答案
要在您喜欢的终端的新实例中运行Python脚本,请输入:
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
这将启动Python脚本并运行它直到结束,然后显示一个Python提示符以停止关闭终端仿真器.
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
这将与 x-terminal-emulator
替换为我计算机上安装的许多终端中的任何一个一起工作,因此在安装了标准终端的所有与POSIX兼容的系统中,只需进行很少的修改即可工作.但是,这不适用于Mac.有关稍微不同的正确跨平台Python实现,请参见此处.大多数技术应该是可移植的.
This will work with x-terminal-emulator
substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
要在同一终端上运行Python脚本,同时继续执行其余的Shell脚本,请编写:
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
注意&
,它将新程序作为程序运行(但仍将输出连接到虚拟终端).
Note the &
, which runs the program as a new process (but still connects the output to the virtual terminal).
这篇关于打开终端,运行python脚本并保持打开状态以获取结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!