简体   繁体   中英

Python: Spawn New Minimized Shell

I am attempting to to launch a python script from within another python script, but in a minimized console, then return control to the original shell.

I am able to open the required script in a new shell below, but it's not minimized:

#!/usr/bin/env python

import os
import sys
import subprocess

pyTivoPath="c:\pyTivo\pyTivo.py"

print "Testing: Open New Console"
subprocess.Popen([sys.executable, pyTivoPath], creationflags = subprocess.CREATE_NEW_CONSOLE)

print
raw_input("Press Enter to continue...")

Further, I will need to be able to later remotely KILL this shell from the original script, so I suspect I'll need to be explicit in naming the new process. Correct?

Looking for pointers, please. Thanks!

Note: python27 is mandatory for this application. Eventually will also need to work on Mac and Linux.

Do you need to have the other console open? If you now the commands to be sent, then I'd recommend using Popen.communicate(input="Shell commands") and it will automate the process for you.

So you could write something along the lines of:

# Commands to pass into subprocess (each command is separated by a newline) 
commands = (
    "command1\n" +
    "command2\n"
)


# Your process
py_process = subprocess.Popen(*yourprocess_here*, stdin=PIPE, shell=True)

# Feed process the needed input
py_process.communicate(input=commands)

# Terminate when finished
py_process.terminate()

The code above will execute the process you specify and even send commands but it won't open a new console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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