jhansenjr
(Fresh Scripter)
2003-05-27 09:46 PM
check if a service is running

How can I tell if a service is running? I tried several of the examples posted but none seem to work correctly. Need to check if "Norton AntiVirus Server" is running. Thanks

Sealeopard
(KiX Master)
2003-05-27 09:53 PM
Re: check if a service is running

Please take a look at the UDF Forum, as it contains a couple of 'check service' UDFs or search the UDF Forum for 'service'.

jhansenjr
(Fresh Scripter)
2003-05-27 10:07 PM
Re: check if a service is running

Those examples seem a little much for a starter.

AllenAdministrator
(KiX Supporter)
2003-05-27 10:10 PM
Re: check if a service is running

Although not elegant and does little else, this will tell you if the service is running...

code:
function servicestate($_service,optional $remotepc)
$objects=GetObject("winmgmts:{impersonationLevel=impersonate}!"+$remotepc+"\root\cimv2")
if not @error=0
exit @error
endif
$services=$objects.ExecQuery('Select * from Win32_Service WHERE Name = "$_service"')
for each $service in $services
$servicestate=$service.state
next
endfunction

For example: servicestate("Spooler") returns "Running"

Just threw this together so please be understanding if it contains a bug or two, but it did work in my simple test above.

[ 27. May 2003, 22:11: Message edited by: Al_Po ]


Sealeopard
(KiX Master)
2003-05-27 10:11 PM
Re: check if a service is running

Then please take a look at the FAQ Forum under How to use UDFs

Sealeopard
(KiX Master)
2003-05-27 10:16 PM
Re: check if a service is running

Al_Po's example requires WMI and will thus only work under Windows 2000/XP/2003 by default.

Also, there are already a total of eight UDFs dealing with this issue usng different approaches.


jhansenjr
(Fresh Scripter)
2003-05-27 10:45 PM
Re: check if a service is running

I see all the approaches but I'm having difficulty following them. I guess I will try to figure it out on my own.

Howard Bullock
(KiX Supporter)
2003-05-27 10:48 PM
Re: check if a service is running

give me a chance to get home and I will walk you through an example. About 5:30 PM EST I will be back online.

AllenAdministrator
(KiX Supporter)
2003-05-27 10:52 PM
Re: check if a service is running

If you have Win2000 or higher the above script should do the trick... What OS are you running?

Another Example...

If servicestate("Norton Antivirus Server") = "Running"
...your code here...
endif


Howard Bullock
(KiX Supporter)
2003-05-27 11:49 PM
Re: check if a service is running

I will be on and off the forum. Let me know if you want to discuss an example.

jhansenjr
(Fresh Scripter)
2003-05-27 11:52 PM
Re: check if a service is running

Sure. I'm looking at one called active service.

JochenAdministrator
(KiX Supporter)
2003-05-28 12:01 AM
Re: check if a service is running

have you tried xnet ?


break on

shell '%ComSpec% /c xnet list \\Server\Service | find /C "Running" >nul 2>nul'
if @error
? "Service is not running"
else
? "Yes, Service runs"
endif

get $_


Xnet is written by Ruud and is since ever then in the KiXtart distribution


Howard Bullock
(KiX Supporter)
2003-05-28 12:09 AM
Re: check if a service is running

ActiveServive(Service) is nothing more than a wrapper for using XNET.exe which is a utility in your KiXtart distribution.

Read the XNET help file. Then look at this code again. It just calls XNET, reports the return and returns a value for NT.

The @WIN = 1 means NT. The else would be for Win9x computers and it checks the registry for Win9x services.


Howard Bullock
(KiX Supporter)
2003-05-28 12:15 AM
Re: check if a service is running

Check out fADSIServiceRun(). This code uses ADSI via COM to work with services. In it is simple code that will show how to get the status of a service.
code:
$objService = Getobject("WinNT://$Computer/$Service,Service")
if @error = 0
$status = $objService.status
;$status = 4 ;Running
;$status = 1 ;Stopped
;$status = 7 ;Paused
endif



[ 28. May 2003, 00:16: Message edited by: Howard Bullock ]


jhansenjr
(Fresh Scripter)
2003-05-28 12:16 AM
Re: check if a service is running

Jochen, thanks. That works fine.

Howard, I understand the points that you brought up but what I didn't underdstand was the $servicename. Where is that being declared? Above this script? Thanks


Howard Bullock
(KiX Supporter)
2003-05-28 12:23 AM
Re: check if a service is running

Usage:
$active=ACTIVESERVICE("WINS")

code:
function activeservice($servicename)
Dim $shellcommand, $xnetexe

$activeservice=0
$servicename=trim($servicename)
if $servicename=''
exit 87
endif
if not isdeclared($TOOLSDIR)
global $TOOLSDIR
endif
...


$servicename in the above example is "WINS" which was passed into the function when used in the script.

The function definition specified what parameters are to be accepted by a function. These parms can be mandatory or optional.

See the FUNCTION command in the docs.

[ 28. May 2003, 00:24: Message edited by: Howard Bullock ]


jhansenjr
(Fresh Scripter)
2003-05-28 12:34 AM
Re: check if a service is running

Thanks Howard. I appreciate your help.

Howard Bullock
(KiX Supporter)
2003-05-28 12:45 AM
Re: check if a service is running

Any other questions?

NTDOCAdministrator
(KiX Master)
2003-05-28 12:47 AM
Re: check if a service is running

Here is a version tailored to what you want to find. Reading and Experimenting will help increase your knowledge as well. When you have time please review the KiXtart manual as well as the FAQ section here on the board. You can also download the HELPFILE from my site (the link is in my signature)

From a DOS prompt on either Windows 2000 or XP :
Using KiXtart v4.21

NOTICE: The remote system must have WMI installed. 2000/XP have it by default, NT 4 does not. Please review this FAQ for further information on this.
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=10;t=000048

C:\SCRIPTS\KIX32.EXE CHECKNAV.KIX


$strComputer = "NameOfComputerToCheck"
$Proc = "Norton Antivirus Server"

BREAK ON
ListProc($strComputer, $Proc)

FUNCTION ListProc($strComputer,$Proc)
For each $Process in GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2").ExecQuery("select * from Win32_Service where Name='$Proc'")
? $PROC +' on '+$strComputer +' is '+$Process.state
Next
ENDFUNCTION


[ 28. May 2003, 00:56: Message edited by: NTDOC ]


Howard Bullock
(KiX Supporter)
2003-05-28 12:49 AM
Re: check if a service is running

Long line police. You're busted DOC

NTDOCAdministrator
(KiX Master)
2003-05-28 12:51 AM
Re: check if a service is running

I'll leave it for noobies. Don't want them to confuse them with where/how lines can be broken.

Howard Bullock
(KiX Supporter)
2003-05-28 12:55 AM
Re: check if a service is running

jhansenjr, you must also realize that service name come in two flavors. short name as used in the registry services key and the display name.

Ths can be easily seen by using SCLIST.exe.

code:
Alerter                          Alerter
AppMgmt Application Management
aspnet_state ASP.NET State Service
Ati HotKey Poller Ati HotKey Poller
AutoProxyService Tyco Electronics AutoProxy Service
AvSynMgr AVSync Manager
BITS Background Intelligent Transfer Service
Browser Computer Browser

Some methods may want the short name and others may want the long name. Some don't care.

Here something not from Microsoft: http://www.jsifaq.com/SUBL/tip5900/rh5936.htm

[ 28. May 2003, 01:01: Message edited by: Howard Bullock ]


jhansenjr
(Fresh Scripter)
2003-05-28 02:23 AM
Re: check if a service is running

Thank you everyone for your help.