Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Start 4 Services Check that they have succesfuly started

1 view
Skip to first unread message

B

unread,
Jul 30, 2002, 8:57:08 PM7/30/02
to
Hi

First I'm new to scripting so thanks for the support. I think this is harder
than the C++ and win32 api. I'm trying to write a script can be shell vbs
or wfs to start a series of four 3rd party services
which will be launched as a cluster resource fails over (non cluster aware
app.). I know how to handle the cluster part, its the script that's kicking
me. Functionality : I need to start each service one at a time.
and put a wait / sleep in for some services that take longer to start .
Then I would like to check that the service actually started before
attempting to start the next sequenced service.
Just to test the start with a dummy service and test the delay from a
"Test.bat " file I wrote:

Net Start Clipsrv
Sleep 500
Net Stop "windows time"

and so on, the service will start but the sleep function fails "SLEEP - no
such command" ( I'm running W2k AS)
a way of testing the return code for success with net start service? It
displays to the command line that the service has started, but I need to
know programmatically if was started.
The Way I would do this in c or C++ is X = function and test the value of
X.
I tried X = net start "service name" and then was going to test the value of
x but it did not like the syntax. I also tried

SC Query "ServiceName"
but you get a big string
There is a query state but fails if I add a service name.

I tried to get the Sleep to work in a VBS file. since it failed in Test.bat

It was also unknown in vbs. So I tried WScript.Sleep 9999, It works and gave
me a ten sec ~~10 sec delay
But Net Start "ServiceName" failed in the VBS script.

looks like I need getobject but don't know how to use it

Can anyone help me out to :

1 start a service
2 delay ....
3 test that the individual service has started

then it repeats for 3 more services,
if you show me one I can do the rest ...duh

feel like an idiot
thanks in advance
brendan


Peter

unread,
Jul 30, 2002, 10:43:21 PM7/30/02
to
In a batch file you can use the return code in the ERRORLEVEL variabel.
The errorlevel variabel returns 0 if the service has started sucessfully, if
it fails it returns 2. Maybe it can return 1, but i have never seen it.


Here is an example:

net start clipsrv
if errorlevel 2 goto error
rem The service has started, let's start next

net start nextservice
if errorlevel 2 goto error

:error
echo Failed....
goto :EOF


"B" <b...@n.com> wrote in message news:3d47...@news.mdc.net...

guy

unread,
Jul 30, 2002, 11:19:24 PM7/30/02
to
If you just want to start your 4 services in order on W2K then why not
set the dependencies accordingly?

Torgeir Bakken

unread,
Jul 31, 2002, 5:33:54 AM7/31/02
to
B wrote:

> First I'm new to scripting so thanks for the support. I think this is harder
> than the C++ and win32 api. I'm trying to write a script can be shell vbs
> or wfs to start a series of four 3rd party services
> which will be launched as a cluster resource fails over (non cluster aware
> app.). I know how to handle the cluster part, its the script that's kicking
> me. Functionality : I need to start each service one at a time.
> and put a wait / sleep in for some services that take longer to start .
> Then I would like to check that the service actually started before
> attempting to start the next sequenced service.
> Just to test the start with a dummy service and test the delay from a
> "Test.bat " file I wrote:

> (snip)

Hi

Net Start will wait (stop the batch file) until the service is started, so
normally you shouldn't need a sleep. For error testing, see further down

If you need a sleep command anyhow, it is a Sleep.exe in the Resource Kit.

If you don't have the Res. Kit, here are some alternatives:

Use ping.exe

ping -n <time> 127.0.0.1>nul

where <time> should be approximately the number of seconds you wish to delay
plus 1.

So a delay for 5 seconds will be like this:

ping -n 6 127.0.0.1>nul

This process consumes very few resources.

Freeware:

UnxUtils has a sleep.exe
http://www.weihenstephan.de/~syring/win32/UnxUtils.html


**********************************
Batch file with error check:

@echo off
net start "DNS Client"
if errorlevel 1 goto e1

net start "Event Log"
if errorlevel 1 goto e2

:e1
Echo Error starting "DNS Client", or it is already started
goto exit

:e2
Echo Error starting "Event Log", or it is already started
goto exit

:exit


--
torgeir


Fosco

unread,
Jul 31, 2002, 11:10:20 AM7/31/02
to
"B"

> 1 start a service
> 2 delay ....
> 3 test that the individual service has started

*.vbs

set WShell = WScript.CreateObject("WScript.Shell")
set oExec = WShell.Exec("NOTEPAD.EXE")

Do While not WShell.AppActivate("Untitled - Notepad")
WScript.Sleep 100
Loop
Msgbox "ACTIVATE"
set oExec = WShell.Exec("CALC.EXE")
WScript.Sleep 1000
WShell.AppActivate("Untitled - Notepad")
WScript.Sleep 1000
WShell.AppActivate("Calculator")
WScript.Sleep 1000
WShell.AppActivate("Untitled - Notepad")
if WShell.AppActivate("Untitled - Notepad") then
WScript.Sleep 1000
WShell.AppActivate("Calculator")
WShell.run"wordpad.exe"
end if
--
Fosco

B

unread,
Aug 1, 2002, 8:02:08 PM8/1/02
to
Thanks to all that replied, I got it to work and learned.
Even updates the eventlog .

thanks
brendan


B

unread,
Aug 2, 2002, 10:23:19 PM8/2/02
to
One caveat - I'm finding that the return only means that the service tried
to start, (stop - starting - Started)

a service like alerter or clip board consistently starts within secs.

The first service I am starting after a cluster resource failover is an
Oracle DB, then other services and agents based on the particulars of the
source crash. I can give it a 2 minute or 20min wait to insure that oracle
db has rebuilt and the service is has changed from STARTING to STARTED ,
time can be anywhere from 1 -20 mins

But the return value from the "start service " is immediately a 0, as soon
as the service toggles to "starting"

I need to poll the service every 5 or 10 secs, until I know the state is
STARTED, then I will proceed to start the other services and agents.
Customer does not want to use dependencies as someone suggested earlier,
that was one of the 1st question I asked. The services that are required to
start are not the same for every instance


"Peter" <sp...@hokksund.myip.org> wrote in message
news:d9I19.4517$sR2....@news4.ulv.nextra.no...

Torgeir Bakken

unread,
Aug 2, 2002, 11:45:47 PM8/2/02
to
B wrote:

> One caveat - I'm finding that the return only means that the service tried
> to start, (stop - starting - Started)

> (snip)


>
> I need to poll the service every 5 or 10 secs, until I know the state is
> STARTED, then I will proceed to start the other services and agents.

Hi

Some ways to do this:

1)
The complicated way: Run SC in a loop and parse the output of a query. It will
be "STATE : 2 START_PENDING" while the service is starting and
"STATE : 4 RUNNING" when it is running:


C:\>sc query messenger

SERVICE_NAME: messenger
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 4 RUNNING
(STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0


2)
The better method is to use ADSI or WMI for this.


ADSI:

Const ADS_SERVICE_START_PENDING = 2
Const ADS_SERVICE_RUNNING = 4

sService = "LanmanServer"
sServer = "." ' "." for local machine

Set oComputer = GetObject("WinNT://" & sServer & ",computer")

On Error Resume Next
Set oService = oComputer.GetObject("Service", sService)
If Err.Number <> 0 Then
MsgBox "Error connecting to service " & sService
WScript.Quit
End If

On Error Goto 0

If oService.Status <> ADS_SERVICE_RUNNING Then
On Error Resume Next
oService.Start
If Err.Number <> 0 Then
MsgBox "Error starting service " & sService
WScript.Quit
End If
On Error Goto 0
WScript.Sleep 200
sStatus = oService.Status
If sStatus = ADS_SERVICE_RUNNING Or _
sStatus = ADS_SERVICE_START_PENDING Then
Do Until oService.Status = ADS_SERVICE_RUNNING
WScript.Sleep 100
Loop
Else
MsgBox "Error starting service " & sService
WScript.Quit
End If
End If

WScript.Echo sService & " started!"


IADsService
http://msdn.microsoft.com/library/en-us/netdir/adsi/iadsservice.asp

IADsServiceOperations
http://msdn.microsoft.com/library/en-us/netdir/adsi/iadsserviceoperations.asp

WMI method:

' Services can have this 4 statuses (and more) in WMI:
' Start Pending
' Running
' Stop Pending
' Stopped

' Pending indicates that the service is in the process of
' beeing started or stopped

set wmi = getobject("winmgmts:")

' using Norton Antivirus client as an example
servicename = "norton antivirus client"

wql = "select state from win32_service " _
& "where displayname='" & servicename & "'"
set results = wmi.execquery(wql)

for each service in results
if not lcase(service.state) = lcase("Running") then
' Start service
returncode = service.StartService
if returncode <> 0 then
msgbox "Error starting service " & servicename
WScript.Quit
end If

do
' Adjust sleep as necessary, but do *not* remove it!
WScript.Sleep 300
wql = "select state from win32_service " _
& "where displayname='" & servicename & "'"
set results2 = wmi.execquery(wql)

for each service2 in results2
WScript.Echo servicename & ": " & service2.state
if lcase(service2.state) = lcase("Running") then
WScript.Echo servicename & ": " & service2.state
started = True
end if
next

loop until started
end if
next

wscript.echo servicename & " started!"


Win32_Service
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_service.asp


--
torgeir


0 new messages