简体   繁体   English

从python打开终端应用程序并在其中运行自定义脚本

[英]Opening a terminal application from python and running custom scripts inside it

I'm working with a software called dc_shell that has a terminal command (also called dc_shell) on a CentOS Linux server. 我正在使用一个名为dc_shell的软件,该软件在CentOS Linux服务器上具有一个终端命令(也称为dc_shell)。 when I run dc_shell command, I'm connected to its terminal and I'm able to run scripts/commands inside it. 当我运行dc_shell命令时,我已连接到其终端,并且能够在其中运行脚本/命令。 (This is all done manually) (这都是手动完成的)

So the real problem is that I want to do this task all from a Python program. 因此,真正的问题是我想全部通过Python程序执行此任务。 Meaning that I have a Python code which does some task, and after that has to open dc_shell and run some commands inside it. 这意味着我有一个执行某些任务的Python代码,此后必须打开dc_shell并在其中运行一些命令。

I have used subprocess.Popen before and this doesn't have any problem when I run commands like ls or other general terminal commands. 我以前使用过subprocess.Popen ,当我运行ls命令或其他通用终端命令时,这没有任何问题。 But when I run dc_shell command it seems like it crashes and nothing happens, and when I try to terminate the session I get the following errors in my terminal. 但是,当我运行dc_shell命令时,它似乎崩溃了,什么也没有发生,当我尝试终止会话时,在终端中出现以下错误。

Here's my code: 这是我的代码:

def run_scripts():
    commandtext = 'cd ..; dc_shell-xg-t; set_app_var link_library "slow.db"; set_app_var target_library "slow.db"; set_app_var symbol_library "tsmc18.sdb";'
    print(commandtext)
    process = subprocess.Popen(commandtext,stdout=subprocess.PIPE, shell=True)
    proc_stdout = process.communicate()[0].strip()
    print(proc_stdout)

and the output is: 输出为:

cd ..; dc_shell-xg-t; set_app_var link_library "slow.db"; set_app_var target_library "slow.db"; set_app_var symbol_library "tsmc18.sdb";

and nothing happens... and after terminating I get: 并没有任何反应...终止后,我得到:

[User@server python]$ /bin/sh: set_app_var: command not found
/bin/sh: set_app_var: command not found
/bin/sh: set_app_var: command not found

Do you need to use dc_shell to run your commands? 您是否需要使用dc_shell来运行命令?

If so, that should be your executable and the rest of commands your arguments. 如果是这样,那应该是您的可执行文件,其余的命令应该是您的参数。

You should never use shell=True due to security considerations (the warning in the 2.x docs for subprocess seems much clearer to me). 出于安全考虑,您永远不要使用shell = True( 2.x文档中有关子流程的警告对我来说似乎很清楚)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM