Post Content

Very handy VBScript, watches a process during a certian time interval, if it's running good, if not start it unless it's beyond the time interval, then kill it. Good if you want a program to run between 1pm-5pm but not the rest of the time. Hook this up to a scheduled task and exec every 10 minutes to ensure app is running or app is dead. IF the process is down in the time frame, it will start it, and send out an email that it was down. See Send Email using VBScript

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

You must edit the script below. Must set the process to watch, the times it should be running, and all the email information. Since all variables are built into the script and not parameter based, just call the script without any parameters.

I use this script to make sure a critical process is running all the time, so setup a scheduled task to run this ProcessManager.vbs every 10 minutes. This insures its always running during the time window, and always NOT running outside the window.

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

ProcessManager.vbs
Option Explicit
'Check a process, restart it and send email alert, or kill process depending on time window

'Original script created by NetRes
'Modified by mReschke 2008-04-28 (converted to functions, actions change by time, email enhancements, globals, generic process killer)
'If process is NOT running between start/endHour then restart it and send an email if sendEmail = true
'If process is running outside of start/endHour then kill it.

'Globals
Dim objProcess, objWMIService, colProcesses, Process, ProcessorTime, pc
Dim strBody, iMsg, iConf, Flds, DataList, strComputer

Dim processName        'Name of process to check/kill/restart    (NOTE: Case Sensetive !!!)
Dim processPath        'Full path to process .exe (do not include name.exe, ex: d:\dynamailgenerator\bin\)
Dim sendEmail        'Bool to send alert email if service is not running
Dim curTime, startHour, endHour, curMin

' ---------------------------------------------------------------------------------
'Set Configs (edit this section)
processName = "notepad.exe"
processPath = "c:\windows\system32\"
sendEmail = true
startHour = 5
endHour = 22

'Determine what to do based on the current time
curTime = CInt(mid(FormatDateTime(Time(), 4), 1, 2))    '24 hour time, just the hour (so 02 or 08 or 12 or 15 or 18)
curMin = CInt(mid(FormatDateTime(Time(), 4), 4, 2))

If curTime >= startHour AND curTime < endHour Then
    'Process should be running between these times (restart it if not)
    If IsRunning = False Then
        'Process is NOT running, restart it and send an alert email
        
        if curTime = startHour AND curMin < 20 Then sendEmail = false    ' don't send alert email the first 20 minutes of the day (because it's just starting up, not really restarting)
        
        if sendEmail = true Then Call SendAlertEmail
        Call RestartProcess
    Else
        'Process is running fine, we are good!
    End If
Else
    'Kill the process if it is running
    Call KillProcess
End if

' ---------------------------------------------------------------------------------
Sub SendAlertEmail()
    Set pc = CreateObject("Wscript.Network")
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields

    Const schema = "http://schemas.microsoft.com/cdo/configuration/"
    dim emailBody

    Flds.Item(schema & "sendusing") = 2
    Flds.Item(schema & "smtpserver") = "143.323.xxx.xxx" 
    Flds.Item(schema & "smtpserverport") = 25
    Flds.Item(schema & "smtpauthenticate") = 0
    'Flds.Item(schema & "sendusername") = "domain\username"
    'Flds.Item(schema & "sendpassword") = "yourpass"
    Flds.Item(schema & "smtpusessl") = 0
    Flds.Update

    With iMsg
        .To = mail@mail.com, mail2@mail.com"
        .From = "Process Alert "
        .Subject = processName & " has quit"

        emailBody = "

'" & processName & "' has unexpectedly quit

" emailBody = emailBody & "Server: " & pc.ComputerName & "
" emailBody = emailBody & "Process: " & processName & "
" emailBody = emailBody & "Location: " & processPath & processName & "
" emailBody = emailBody & "Time: " & Now & "

" emailBody = emailBody & "Attempting to restart '" & processName & "'

" emailBody = emailBody & "Please check that the process has restarted correctly.
" 'emailBody = emailBody & " .HTMLBody = emailBody .Sender = "Process Alert " .Organization = "Your Company" .ReplyTo = "replyhere@mail.com" Set .Configuration = iConf .Send End With ' Release Interfaces Set iMsg = nothing Set iConf = nothing Set Flds = nothing End Sub ' --------------------------------------------------------------------------------- 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 RestartProcess() 'Restart 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