• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 922
  • Last Modified:

Batch, vbscript to start/end process

This question is not programming related, but to go around to resolve the problem by batch or vbscript to start/end process.

I have a very old custom application which is not updated anymore. It checks XML file at remote site every 5 minutes. I need to make sure it runs all the time. Recently we have an issue it throws 'Run Time' error.

When I look at the log, it says 'Invalid Argument' then the process throws the error.

Is there a way for any script to contiguously monitor this log and if it encounters 'invalid argument', then it forces to end the process and restart? After the application is restarted, it doesn't require user interaction such as clicking button or any.
  • 4
  • 3
  • 2
4 Solutions
Can you post an example of the log file?
Bill PrewCommented:
Agreed, it would be best to figure out what is causing the error and try to eliminate that.  But if that's not possible...

Are you sure the process doesn't end when it throws the error?  If that were the case, then you could just check every few minutes if the process is still running, and if not restart it.

Of course you could write a BAT or VBS script that looks for the log file, and if found with the error, deletes the log file, and stops and restarts the process.  That would not be that hard in BAT.  You could then schedule that to run on some frequency from Windows Task Scheduler and it could check each time it runs.

To create the batch script I would need some additional info, like where will the log file be located, and example of it's format, including the error message, what the process name and info is, how to restart it, etc.

crcsupportAuthor Commented:
As I said, The software developer is not available to source code to fix this. Also, I thought this is caused by the XML data provider to the application, but the company denies any change made to the xml data format, which I strongly believe, but contacted 3 times, seem as they are rock hard on saying no change. .
Anyway, the option is I have to have script runs on background to check the below string in the log every 5 minutes or more and stop/start the aplication (abc.exe).

GetFlightDetails>>3001 - At line [3390] Invalid argument.
As soon as it writes this string line, the application stops showing 'Runtime 3390' with popup. Then it stops working. So I normally click 'OK', then restart the abc.exe application. The flow of doing this automatically is

1. background script checks the log file with '[3390] Invalid argument' every 5 minute, if there is, force to close abc.exe process.
2. Then restart abc.exe

I got a few examples of scripts to terminate process. I think I can just reuse them modifying it. But can you give me some idea or example of codes which will run all the time and check line string in log file every x minute?
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

I would suggest using an __InstanceModificationEvent (http://msdn.microsoft.com/en-us/library/aa394651.aspx)

http://stackoverflow.com/questions/10041057/how-to-monitoring-folder-files-by-vbs gives an example script on how to do this.  It will 'watch' a folder, based on a value every n seconds.  You can filter based on the file name of the log file.  That would tell you that the log was changed.  You would just need to go through the log to find that text string.  Since you haven't posted a sample log, I can't really help much further than that.
The scripting guy has a good example on how to do this... http://blogs.technet.com/b/heyscriptingguy/archive/2007/03/29/how-can-i-search-a-text-file-for-strings-meeting-a-specified-pattern.aspx
crcsupportAuthor Commented:
Thanks for the link. I also posted here, the log file and screen shot of the log folder structure. Basically once it encounters "3001 - At line [3390] Invalid argument.", it stops logging in the log file and creates another logfile with date.

log folderlogfile.txt
Repurposing some of those scripts already posted, something like this could work:
If as stated in the code comments, you simply need to watch for the file creation (sounds like it from your description above), then you could fire the RecycleProcess sub at that point (that sub needs to be adjusted with your process name/location, as well as the log path on the first line...

sPath = "C:\logs\"
sComputer = "."
sDrive = Left(sPath,2)
sFolders = REPLACE(sPath, "\", "\\")

Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' AND " _
& "TargetInstance.Drive='" & sDrive & "' AND " _
& "TargetInstance.Path='" & sFolders & "' AND " _
& "TargetInstance.Extension = 'log' ")

Wscript.Echo vbCrlf & Now & vbTab & _
"Begin Monitoring for " & sPath & "'s Change Event..." & vbCrlf

    Set objLatestEvent = colMonitoredEvents.NextEvent
    Select Case objLatestEvent.Path_.Class

    Case "__InstanceCreationEvent"
    'TODO:  Based on your description, if simply hitting the error triggers a new
	'log file, you could trap for this event, and perform your process recycle

    'WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    '& " was created" & vbCrlf

    'Case "__InstanceDeletionEvent"
    'WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    '& " was deleted" & vbCrlf

    Case "__InstanceModificationEvent"
    'Otherwise, you'd need to open the file, check for the error, and then perform the process recycle
    If objLatestEvent.TargetInstance.LastModified <> _
    objLatestEvent.PreviousInstance.LastModified then
    WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    & " was modified" & vbCrlf
    End If
    End Select

Set objWMIService = nothing
Set colMonitoredEvents = nothing
Set objLatestEvent = nothing

Sub RecycleProcess
  Dim objShell : Set objShell = CreateObject ("Wscript.Shell")
  objShell.Run "taskkill /im MyApplication.exe /f"
  wscript.sleep 1000
  objShell.Run "start c:\Folder\MyApplication.exe"
End Sub

Open in new window

crcsupportAuthor Commented:
Thanks all. This is enough for me to start.
Happy to help - thanks for the grade! :^)
Bill PrewCommented:
Thanks for the feedback.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now