2009-06-02

Respawn a process

I have one program that is somewhat unstable and dies from time to time (not a service).

To make sure that the program is running most of the time, I made a small VBScript using WMI to check if the process is running, and if not, respawn it.

Here is my code:
' Respawns a process if it is not running
' Can be used in scheduled tasks.
'
' Known issuses: If the program is a GUI application, the user running the
' scheduled task should be logged in.
'
' Copyright (c) 2009 Rune Nordbøe Skillingstad <rune.skillingstad@ntnu.no>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; version 2 dated June, 1991.
'
' This program is distributed in the hope that it will be useful, but
' WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
' General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
' USA.
'
Option Explicit

On Error Resume Next

Dim ProcName, ProcPath
Dim objWMIService, colItems, objShell

' Change this to your program
ProcName = "Program.exe"
ProcPath = "C:\Path"

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & ProcName & "'")

If colItems.Count = 0 Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run ProcPath & "/" & ProcName, 1, false
Set objShell = Nothing
End If

Set colItems = Nothing
Set objWMIService = Nothing


I added this as a scheduled task. Just follow the wizard, finish, edit and click advanced schedule like this screen shot:

No comments:

Post a Comment