Solved

Restart a failed program

Posted on 2006-11-28
7
148 Views
Last Modified: 2010-04-30
I am running Mercury mail on my windows PC.
Occasionally it will stop running.
I need a script that I can schedule to run every 30 minutes that will:

1.  check the running processes for mercury.exe
2.  If mercury.exe is in the running processes, do nothing and go back to sleep
3.  If mercury.exe is not found in the running prcesses, restart mercury.exe (i.e. c:\mercury\mercury.exe)
4.  Rerun step 2
4.  Output actions to a log file

Any suggestions?
0
Comment
Question by:greetrufus
  • 2
  • 2
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18028655
You can do something like...

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")

' launch it...
Set oExec = WshShell.Exec("calc")

Dim targetTime
targetTime = DateAdd("n", 30, Now) ' 30 minutes from now

While True
    ' check every 5 seconds to see if our targetTime has been passed
    While targetTime > Now
        WScript.Sleep 5000 ' (max is 32,767 --> approx 30 seconds)
    Wend

    ' if it's closed...run it again
    If oExec.Status <> 0 Then
        Set oExec = WshShell.Exec("calc")
    End If

    ' set next "event" for 30 minutes out...
    targetTime = DateAdd("n", 30, Now) ' 30 minutes from now
Wend
0
 

Author Comment

by:greetrufus
ID: 18028858
Any way to output the results to a log file?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18028986
Something like...

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim fileName, fso, f, lines

fileName = "c:\someLog.txt"
Set fso = CreateObject("Scripting.FileSystemObject")

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")

' launch it...
Set f = fso.OpenTextFile(fileName, ForAppending, True)
Set oExec = WshShell.Exec("calc")
f.Write(Now & " Application Started" & vbCrLf)  
f.Close

Dim targetTime
targetTime = DateAdd("n", 30, Now) ' 30 minutes from now

While True
    ' check every 5 seconds to see if our targetTime has been passed
    While targetTime > Now
        WScript.Sleep 5000 ' (max is 32,767 --> approx 30 seconds)
    Wend

    ' if it's closed...run it again
    Set f = fso.OpenTextFile(fileName, ForAppending, True)
    If oExec.Status <> 0 Then
        Set oExec = WshShell.Exec("calc")
        f.Write(Now & " Application Restarted" & vbCrLf)  
    Else
        f.Write(Now & " Application Still Alive" & vbCrLf)  
    End If
    f.Close

    ' set next "event" for 30 minutes out...
    targetTime = DateAdd("n", 30, Now) ' 30 minutes from now
Wend
 
0
 

Author Comment

by:greetrufus
ID: 18029228
How do I set the path to the executable?
it is c:\mercury\mercury.exe

Here is the code I am using that i get a path error on:
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim fileName, fso, f, lines

fileName = "c:\Log.txt"
Set fso = CreateObject("Scripting.FileSystemObject")

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")

' launch it...
Set f = fso.OpenTextFile(fileName, ForAppending, True)
Set oExec = WshShell.Exec("c:\mercury\mercury.exe")
f.Write(Now & " Application Started" & vbCrLf)  
f.Close

Dim targetTime
targetTime = DateAdd("n", 30, Now) ' 30 minutes from now

While True
    ' check every 5 seconds to see if our targetTime has been passed
    While targetTime > Now
        WScript.Sleep 5000 ' (max is 32,767 --> approx 30 seconds)
    Wend

    ' if it's closed...run it again
    Set f = fso.OpenTextFile(fileName, ForAppending, True)
    If oExec.Status <> 0 Then
        Set oExec = WshShell.Exec("c:\mercury\mercury.exe")
        f.Write(Now & " Application Restarted" & vbCrLf)  
    Else
        f.Write(Now & " Application Still Alive" & vbCrLf)  
    End If
    f.Close

    ' set next "event" for 30 minutes out...
    targetTime = DateAdd("n", 30, Now) ' 30 minutes from now
Wend
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 70
how to know if my Checkbox is True in VB6.0? 9 65
MsgBox 4 71
VBA: copy range dynamically based on config sheet v3 6 23
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

680 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