简体   繁体   English

从 Python 运行 shell 并使其处于活动状态

[英]Run shell from Python and make it active

I use Python scripts to prepare some stuff for long quantum calculations.我使用 Python 脚本为长量子计算准备一些东西。 So, when I run target binary from terminal所以,当我从终端运行目标二进制文件时

targetbin file.inp > file.out

I can see that process is still running.我可以看到该进程仍在运行。 With Python I do some automations:使用 Python 我做了一些自动化:

#!/usr/bin/env python3

# blah blah blah

cmd = ' '.join([targetbin, modified_inp_file, some_options, '>', created_out_file])
# /usr/bin/targetbin /tmp/XXX.inp -a 1 -b 2 -c 3 > file.out

subprocess.Popen(cmd, shell=True)

run it from terminal:从终端运行它:

pythonscript -x 1 -y 2 -z 3 file.inp

and it works fine, but it doesn't hold shell.它工作正常,但它不持有外壳。 After Python initiate the process I want to delegate it completely to the shell and then exit Python itself, as if I entered cmd myself.在 Python 启动进程后,我想将它完全委托给 shell,然后退出 Python 本身,就好像我自己输入了cmd Is it possible?是否可以?

You can try this way -你可以试试这个方法——

  1. Run a shell script that run the python script.运行运行 python 脚本的 shell 脚本。
  2. Python will do the automations and will run the the process run by POPEN. Python 将执行自动化并运行 POPEN 运行的进程。
  3. Shell script will do fg . Shell 脚本将执行fg (Alternatively, you can get the PID from python and run kill -CONT <pid> ). (或者,您可以从 python 获取PID并运行kill -CONT <pid> )。

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

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