Vikram
Vikram

Reputation: 61

Run a .bat program in the background on Windows

I am trying to run a .bat file (which acts as a simulator) in a new window, 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.

Upvotes: 6

Views: 9210

Answers (4)

Artur Gaspar
Artur Gaspar

Reputation: 4552

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.

Upvotes: 3

Asi
Asi

Reputation: 126

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()

Upvotes: 1

Michael Dillon
Michael Dillon

Reputation: 32392

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

Upvotes: 1

Patrick Perini
Patrick Perini

Reputation: 22633

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"

Upvotes: 2

Related Questions