Need Batch/Script file to run program

Posted on 2007-07-21
Last Modified: 2010-08-05
We have a small exe program running on a Win2003 server which is necessary for some of our legacy applications to operate.  For some unknown reason, this required exe file stops running.  While we are attempting to discover the reason the program stops, I want to run a simple batch/script file which will: see if the program is running, and if not, run it.
I could set this batch file up as a scheduled task to run every 5 minutes.
Question by:Rockit55
    LVL 65

    Accepted Solution

    You can try using the VBS file, which constantly runs, monitoring an EXE process from Task Manager.
    To end the script, you need to go to the Task Manager, and end the WScript.exe process.
    Just change Notepad.exe to the name of the program executable, and see if it works.

    strComputer = "."
    Set objShell = WScript.CreateObject("Wscript.Shell")
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    boolStillRunning = True

    Do While boolStillRunning = True
          Set colProcess = objWMIService.ExecQuery ("Select Name from Win32_Process")
          boolStillRunning = False
          For Each objProcess in colProcess
                If objProcess.Name = "notepad.exe" Then
                      boolStillRunning = True
                End If
          Wscript.Sleep 5000
          If boolStillRunning = False Then
                objShell.Exec "notepad.exe"
                boolStillRunning = True
          End If


    LVL 74

    Expert Comment

    by:Jeffrey Kane - TechSoEasy
    If that doesn't work, have you considered running your legacy apps on a virtual server running on top of your SBS?  There's great documentation for it here:

    I've done this a number of times and it usually works so much better to let the legacy apps run on older OS's than new ones.  (Although ideally it's better to dump the legacy apps)  :-)


    Author Comment

    After replacing "notepad.exe" with the file we need to run and saving the code to a text file with a .vbs seems to be running flawlessly.
    Thanks for the help!

    LVL 65

    Expert Comment

    No problem.  I forgot to mention, there is a line in there that says
    WScript.Sleep 5000
    which makes the script wait only five seconds to check if the program's not running, so if you feel like you want to check only every minute, use
    WScript.Sleep 60000



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
    You may have discovered the 'Compatibility View Settings' workaround for making your SBS 2008 Remote Web Workplace 'connect to a computer' section stops 'working around' after a Windows 10 client upgrade.  That can be fixed so it 'works around' agai…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now