开发者

Run a .bat program in the background on Windows

开发者 https://www.devze.com 2023-03-14 15:59 出处:网络
I am trying to run a .bat file (which acts as a simulator) in a new windo开发者_Go百科w, so it must always be running in the background. I think that creating a new process is the only option that I h

I am trying to run a .bat file (which acts as a simulator) in a new windo开发者_Go百科w, so it must always be running in the background. I think that creating a new process is the only option that I have. Basically, I want my code to do something like this:

    def startSim:
        # open .bat file in a new window
        os.system("startsim.bat")
        # continue doing other stuff here
        print("Simulator started")

I'm on Windows so I can't do os.fork.


Use subprocess.Popen (not tested on Windows, but should work).

import subprocess

def startSim():
    child_process = subprocess.Popen("startsim.bat")

    # Do your stuff here.

    # You can terminate the child process after done.
    child_process.terminate()
    # You may want to give it some time to terminate before killing it.
    time.sleep(1)
    if child_process.returncode is None:
        # It has not terminated. Kill it. 
        child_process.kill()

Edit: you could also use os.startfile (Windows only, not tested too).

import os

def startSim():
    os.startfile("startsim.bat")
    # Do your stuff here.


Looks like you want "os.spawn*", which seems to equate to os.fork, but for Windows. Some searching turned up this example:

# File: os-spawn-example-3.py

import os
import string

if os.name in ("nt", "dos"):
    exefile = ".exe"
else:
    exefile = ""

def spawn(program, *args):
    try:
        # check if the os module provides a shortcut
        return os.spawnvp(program, (program,) + args)
    except AttributeError:
        pass
    try:
        spawnv = os.spawnv
    except AttributeError:
        # assume it's unix
        pid = os.fork()
        if not pid:
            os.execvp(program, (program,) + args)
        return os.wait()[0]
    else:
        # got spawnv but no spawnp: go look for an executable
        for path in string.split(os.environ["PATH"], os.pathsep):
            file = os.path.join(path, program) + exefile
            try:
                return spawnv(os.P_WAIT, file, (file,) + args)
            except os.error:
                pass
        raise IOError, "cannot find executable"

#
# try it out!

spawn("python", "hello.py")

print "goodbye"


On Windows, a background process is called a "service". Check this other question about how to create a Windows service with Python: Creating a python win32 service


import subprocess
proc = subprocess.Popen(['/path/script.bat'], 
                        stdout=subprocess.PIPE, 
                        stderr=subprocess.STDOUT)

Using subprocess.Popen() will run the given .bat path ( or any other executable).

If you do wish to wait for the process to finish just add proc.wait():

proc.wait()
0

精彩评论

暂无评论...
验证码 换一张
取 消