Post Content

This script will start or stop a process by process name. If the process is already running with a start is called, it will not start a duplicate. The benefit is that it will NOT start the script twice. So if you want to ensure an application is running all the time, just setup a scheduled task to run this script every 10 minutes, if the .exe dies it will start it, if the .exe is still running, it won't start a duplication.

Usage[-][--][++]

Example.bat
REM Example Script.bat (note I path to the .exe dir so it runs in its locat folder)
d:
cd \Directory\Or\EXE\Process
C:\myscripts\StartProc.vbs d:\Directory\To\EXE\Process\ ProcessName.exe start

Script[-][--][++]

StartStopProc.vbs
Option Explicit
'mReschke fo zabbix 2009-03-12
'Will start/stop a process
'Will only start 1 process, will not start if already started, no multiples

Dim ArgObj, strComputer, objWMIService, colProcesses, Process
Dim processPath, processName, processAction
Set ArgObj = WScript.Arguments 

processPath = ArgObj(0) 'Command parameter: full path (no filename) to executable (with \ at end)
processName = ArgObj(1) 'Command parameter: file name (no fullpath) of executable
processAction = ArgObj(2) 'Command parameters (start or stop)

'Example Script.bat (note I path to the .exe dir so it runs in its locat folder)
'd:
'cd \Generator\bin
'd:\scripts\StartProc.vbs d:\Generator\bin\ Generator.exe start

If IsRunning = True Then
    If processAction = "stop" Then
        Call KillProcess
    End IF
Else
    If processAction = "start" then
        Call StartProcess
    End If
End If

Function IsRunning()
    Dim processRunning
    processRunning = false
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & processName & "'")
    For Each Process in colProcesses
        If Process.Name = processName Then 
            processRunning = True
        End If
    Next
    IsRunning = processRunning    'return processRunning (true or false)
End Function

Sub StartProcess()
    'Start the Process
    Dim oShell
    Set oShell = WScript.CreateObject ("WSCript.shell")
    oShell.run "" & processPath & processName & ""
    Set oShell = Nothing
End Sub

Sub KillProcess()
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & processName & "'")
    For Each Process in colProcesses
        If Process.Name = processName Then 
            Process.Terminate()
        End If
    next
End Sub