Link to home
Start Free TrialLog in
Avatar of Heins
HeinsFlag for Australia

asked on

Restart a service when a programe Hangs

Hi all,

I am trying to restart a service if a particular command line program hangs / gives out error. The program, if successful will give out(into stdout )  "Host ....."

What i had come with is on the "code segment".
I use "Set objExecObject = objShell.Exec(strCommand)"  to call the program.
(Where strCommand in this case is err.exe which just prints the key word into the stdout/stderr . it prints "Error: asdfasf"  or "Host .................." . I edit the program and rebuild it every time as per the requirement. )

But the problem is, if the program(err.exe) hangs, it will not execute the next line rather it waits for the program finishes.

In reality the program i would be using is fusping (a Cyberfusion custom program  to test the server)

key word to look for = "Error :"
a reverse logic can be to look for the Keyword "Host"

if the program hangs : Nothing comes out. and waits

Can any one help me to bypass this issue.

OS : win 2k server ( I think it should work on any thing from win 2k / xp )
Option Explicit
Dim intShortSleep, intLongSleep, strService
dim objShell, objExecObject, strOutput
Dim strCommand
dim intFound
 
strService = "Messenger"  ' Messenger is just an example'
 
intShortSleep = 5000
intLongSleep = 15000  '1000 = 1 second '
strCommand = "err"  ' err is just an exe , made using a C program, in reality , it will be fusping of cyberfusion '
 
logExec (strCommand)
 
Msgbox intFound & "  Before the loop ... clear it " ' Just a message box to test the looping can ignore this'
Do Until  intFound =0
	Restart()
	Wscript.Sleep intLongSleep
	intFound=0
	logExec(strCommand)
	Msgbox intFound &  " Inthe loop ..Clear it"
	
loop
Msgbox "bye" ' Remove before implimentation'
 
Sub logExec(strCommand)
  
  
  Set objShell = WScript.CreateObject("WScript.Shell")
  Set objExecObject = objShell.Exec(strCommand)
  Do Until objExecObject.Stdout.AtEndOfStream
     strOutput = objExecObject.Stdout.ReadLine()
     ' syntax : InStr([start,]string1,string2[,compare])'
	 intFound=intFound + InStr(strOutput,"Error")
	
    
  loop
   strOutput=""
  end sub
 
Sub Restart()
' Restart Service'
objShell.Run "net stop " & strService
Wscript.Sleep intShortSleep
objShell.Run "net start " & strService
Wscript.Sleep intLongSleep
End Sub

Open in new window

Avatar of Heins
Heins
Flag of Australia image

ASKER

I think it deserves a higher point ...
ASKER CERTIFIED SOLUTION
Avatar of jake072
jake072
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Heins

ASKER

Dear Jake

Thanks a lot for the comment . I did think of this logic. But, since I am not that of an expert in scripting , can you throw some light on how to create a timer and start a different thread. What really happens is that , I am not sure, how to make the program counter to execute next line of code, when the execution previous one is not complete.

In addition, My understanding of script is that, it is like shell commands, which is executed line by line and not like a compilable program . it would be great, if you could give a small example to help me on this.

I looked into the "settimer" one but i thought that, it would work similar to sleep , but can just call a program or function when timer times out. If, in that case as well, the program does not execute next line.

But I know there should be some way as the scripts are supposed to handle this as a part of automation of human action.

Just to be more specific about the issue . In real time this is how manual method is   :

1 . In command prompt , navigate to the folder in which the "Program" (Fusping) exists.
2. run fusping as  fusping <serverHostname> in  most cases localhost.
3. it returns with the name of the host and status of the service along with name of the existing patch.
                           Or
  It returns an error saying "Error:" so and so service failed to respond or some thing like that.

4 . Irrespective of what ever error it is . stop / kill the services and wait for around 15 sec and start the service back again. 99.99% the service will run and everything will be fine.
5. to verify , run the fusping (step 1 & 2) again and make sure it returns no error.

whole these can be achieved by the above script with small modification. But on a worst case , which is 5-10 % of the cases , the program fusping itself hangs. ie. it comes up with neither error nor an output.  Fixing this is simple , terminate fusping and restart the services and eveything works fine.
   
Hope that would ring a bell in you to suggest me something to automate this. even ready to change the whole logic but just help me to achieve that . It had been a pain for a long time.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Heins

ASKER

Jake ,

You are a genius , I will try it when i reach work and update you on the status .Thanks a lot for  the help. Before closing this question  i will update you about this for sure.