Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

VB Script: restart service function

Hello experts,

I use the folowing function in order to restart tomcat.

Sub DoRestart()
   Dim objShell

   Set objShell = CreateObject("WScript.Shell")

   ' Stop tomcat
   objShell.Run "net stop Tomcat7"   	
   ' Wait 60 seconds for stop to process
   Wscript.Sleep 60000

   ' Start tomcat
   objShell.Run "net start Tomcat7"
End Sub

Open in new window


I would like to improve the function by adding the following requirements:
-If the tomcat status is running perform the stop action and continue with the start action.
-If the tomcat status is blank or is stopping (not running) perform just the start action.

If you have question, please contact me.

Thank you very much for your help.
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Any reason that you are not using PowerShell?
$ServiceName = 'Tomcat7'
$arrService = Get-Service -Name $ServiceName

	if ($arrService.Status -ne 'Running')
	{
		Start-Service $ServiceName
	}
	else
	{
		Restart-Service $ServiceName
	}

Open in new window

Avatar of Luis Diaz

ASKER

Thank you very much for this.

How should we adapat this in vbscript?

Thank you very much for your help.
Why not
Sub DoRestart()
   Dim objShell

   Set objShell = CreateObject("WScript.Shell")

   'Restart tomcat
   objShell.Run "net stop Tomcat7 && net start Tomcat7"   	
End Sub

Open in new window

SOLUTION
Avatar of Bill Prew
Bill Prew

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
@Bill: Thank you very much for this proposal.
I have another think that can be optimized related to the pause performed between stop and start.

Is it possible to perform the start action just right after that we are sure that the status of the service is blank and it is not currently stopped.

This will allows us to remove the approximative pause between the stop and the start?
Avatar of Bill Prew
Bill Prew

Notice that I moved the pause into the "if running" check, so it will only pause if it is running and have to restart.  No pause if not running and just starting.

~bp
Exactly and that is what I want to optimize the "pause" which is approximative and it depends of the server in which you launch the function.

Because sometimes it can be 60 seconds and sometimes it can be 45 seconds. Instead of performing the pause do you think there is a way, to add a ISstopped function in order to be sure that the service is completely stopped and the procedure can go ahead with the start action?
You do not need to pause/wait with
net stop Tomcat7 && net start Tomcat7

Open in new window

@Shaun: I prefer vbscript in that case as this function is part of a subroutines which call multiple vbscript functions.
You do not need to pause/wait with and you do not need to check service state
net stop Tomcat7 && net start Tomcat7

Open in new window

This is VBScript. Sorry use one &
Sub DoRestart()
   Dim objShell

   Set objShell = CreateObject("WScript.Shell")

   'Restart tomcat
   objShell.Run "net stop Tomcat7 & net start Tomcat7"   	
End Sub

Open in new window

Perhaps something like this?

Sub DoRestart()
   Dim objShell, intMaxTries

   Set objShell = CreateObject("WScript.Shell")

   If IsRunning("Tomcat7") Then
	   ' Stop tomcat
	   objShell.Run "net stop Tomcat7"   	
   End If

   ' Try and wait until it stops
   intMaxTries = 60
   Do While IsRunning("Tomcat7")
	  intMaxTries = intMaxTries - 1
	  If intMaxTries = 0 Then
		  Exit Sub
	  End If
      Wscript.Sleep 1000
   Next

   ' Start tomcat
   objShell.Run "net start Tomcat7"
End Sub

Function IsRunning(strService)
	Dim objWMI, strQuery
	strQuery = "Select * from Win32_Service Where Name = '" & strService & "' and state='Running'"
	Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	If objWMI.ExecQuery(strQuery).Count > 0 Then
		IsRunning = True
	Else
		IsRunning = False
	End If
End Function

Open in new window

~bp
Here is resulting command demo
http://somup.com/cbVv2ULwY
@Shaun,

Unfortunately the NET STOP command is not guaranteed to wait until the shutdown is completed.  It waits for a period of time and then will timeout and return to the command line even if the service hasn't finished stopping yet.  Not well documented, but learned over the years...

Anecdotal evidence can be found here.

~bp
@Bill thank you very much for your help. I tested your last proposal but I have an error message related to the "Next"?

Unexpected "Next"?

Do you know why?

Thank you very much for your help.
Do While IsRunning("Tomcat7")
        intMaxTries = intMaxTries - 1
        If intMaxTries = 0 Then
              Exit Sub
        End If
      Wscript.Sleep 1000
  Next

Shouldn't be replace by

   Do While IsRunning("Tomcat7")
        intMaxTries = intMaxTries - 1
        If intMaxTries = 0 Then
              Exit Sub
        End If
      Wscript.Sleep 1000
   Loop


Thank you again for your help.
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
Thank you very much for your proposal. I tested the last version and it works well when the service is not running however when the service is  running it doesn't wait when the service is completely stopped? I put intMaxTries = 100 and even with that it is not able to launch the start once it has completely finish the stopped.
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
Thank you very much for this proposal.

I tested with debug mode and I found the following:

Service is running, performing NET STOP (This is ok)

Service is stopped, performing NET START (This is not ok as this action is performed once the service is still stopping, and not completely stopped).
Odd, I used the same code here on a different service (I don't have tomcat installed) and it seemed to work fine, giving me the following.  Perhaps there is something interesting about Tomcat7 service.  Have you given any thought to using the tomcat commands to stop and restart it?

Service is running, performing NET STOP.
Service still running, waiting - 60
Service is stopped, performing NET START.

Open in new window

~bp
Thank you very much for your help.
I am launching the script as admin with cmd and the problem is that between action 2
"Service still running, -60" and once I click on Ok the net start is done immediately.

I performed a video to show you.

Thank you very much for your help.
restart-tomcat.zip
Running the test with WSCRIPT causes those popups to wait for you to click on them.  It would be better for you to run your test using CSCRIPT.

Even though, it shouldn't have moved on to the START until it was stopped.  That being said I suspect the problem is a small one.  Right now my check for if the service is active looks for a service in the "Running" status.  I suspect as soon as the STOP has been executed the state changes to "Stopping" for a while, before ultimately becoming "Stopped".  We can adjust the code a bit for that...

~bp
Thank you very much for your help. The service is completely stopped once there is a a blank in the status as a result the net start should be send once there is no anymore stopping and stopped status but when the service appears with blank like this:

User generated image
I don't know if we can modifiy the code in particular for this part to perform the start as soon as and as long as the status service appears with blank and not when it is with the status "stopped", "stopping" or "running".

Thank you very much for your help.
If you do the following at a command line when tomcat is stapped as you showed above, what does it display?

sc query tomcat7

Open in new window

~bp
ASKER CERTIFIED 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
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
I am sorry, unable to test it today.
I will take the time to test it this week.
Okay, thanks for the update.

~bp
Tested and it works!!
Thank you again for your help!!!