Skip to content

Instantly share code, notes, and snippets.

@mreschke
Created February 2, 2016 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mreschke/c1d6fd59b6bcc076b9cb to your computer and use it in GitHub Desktop.
Save mreschke/c1d6fd59b6bcc076b9cb to your computer and use it in GitHub Desktop.
Start or stop windows processes if not running or running during time window.
Option Explicit
'Process Manager
'Check a process, restart it and send email alert, or kill process depending on time window
'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.
'Original script created by NetRes
'Modified by mReschke 2008-04-28 (converted to functions, actions change by time, email enhancements, globals, generic process killer)
'Globals
On Error Resume Next
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
processName = "VFIOpenImport.exe"
processPath = "D:\Production\bin\VFIOpenImport\bin\"
sendEmail = true
'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))
startHour = 5
endHour = 21
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") = "smtp.example.com"
Flds.Item(schema & "smtpserverport") = 465
Flds.Item(schema & "smtpauthenticate") = 1
Flds.Item(schema & "sendusername") = "user@example.dom"
Flds.Item(schema & "sendpassword") = "password"
Flds.Item(schema & "smtpusessl") = 1
Flds.Update
With iMsg
.To = "mreschke@dynatronsoftware.com"
.From = "Process Alert <administrator@example.com.com>"
.Subject = processName & " has quit"
emailBody = "<font color='red'><h2>'" & processName & "' has unexpectedly quit</h2></font>"
emailBody = emailBody & "<b>Server: </b>" & pc.ComputerName & "<br />"
emailBody = emailBody & "<b>Process: </b>" & processName & "<br />"
emailBody = emailBody & "<b>Location: </b>" & processPath & processName & "<br />"
emailBody = emailBody & "<b>Time: </b>" & Now & "<br /><br />"
emailBody = emailBody & "<b>Attempting to restart '" & processName & "'</b><br /><br />"
emailBody = emailBody & "<font color='#808080'>Please check that the process has restarted correctly.<br />"
'emailBody = emailBody & "
.HTMLBody = emailBody
.Sender = "Process Alert <administrator@example.com>"
.Organization = "Example"
.ReplyTo = "mail@example.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment