Page 1 of 2 12>
Topic Options
#40791 - 2003-05-27 09:46 PM check if a service is running
jhansenjr Offline
Fresh Scripter

Registered: 2002-08-31
Posts: 37
Loc: New Jersey
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
Top
#40792 - 2003-05-27 09:53 PM Re: check if a service is running
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Please take a look at the UDF Forum, as it contains a couple of 'check service' UDFs or search the UDF Forum for 'service'.
_________________________
There are two types of vessels, submarines and targets.

Top
#40793 - 2003-05-27 10:07 PM Re: check if a service is running
jhansenjr Offline
Fresh Scripter

Registered: 2002-08-31
Posts: 37
Loc: New Jersey
Those examples seem a little much for a starter.
Top
#40794 - 2003-05-27 10:10 PM Re: check if a service is running
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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 ]

Top
#40795 - 2003-05-27 10:11 PM Re: check if a service is running
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Then please take a look at the FAQ Forum under How to use UDFs
_________________________
There are two types of vessels, submarines and targets.

Top
#40796 - 2003-05-27 10:16 PM Re: check if a service is running
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
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.
_________________________
There are two types of vessels, submarines and targets.

Top
#40797 - 2003-05-27 10:45 PM Re: check if a service is running
jhansenjr Offline
Fresh Scripter

Registered: 2002-08-31
Posts: 37
Loc: New Jersey
I see all the approaches but I'm having difficulty following them. I guess I will try to figure it out on my own.
Top
#40798 - 2003-05-27 10:48 PM Re: check if a service is running
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40799 - 2003-05-27 10:52 PM Re: check if a service is running
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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

Top
#40800 - 2003-05-27 11:49 PM Re: check if a service is running
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
I will be on and off the forum. Let me know if you want to discuss an example.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40801 - 2003-05-27 11:52 PM Re: check if a service is running
jhansenjr Offline
Fresh Scripter

Registered: 2002-08-31
Posts: 37
Loc: New Jersey
Sure. I'm looking at one called active service.
Top
#40802 - 2003-05-28 12:01 AM Re: check if a service is running
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
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
_________________________



Top
#40803 - 2003-05-28 12:09 AM Re: check if a service is running
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40804 - 2003-05-28 12:15 AM Re: check if a service is running
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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 ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40805 - 2003-05-28 12:16 AM Re: check if a service is running
jhansenjr Offline
Fresh Scripter

Registered: 2002-08-31
Posts: 37
Loc: New Jersey
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

Top
#40806 - 2003-05-28 12:23 AM Re: check if a service is running
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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 ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40807 - 2003-05-28 12:34 AM Re: check if a service is running
jhansenjr Offline
Fresh Scripter

Registered: 2002-08-31
Posts: 37
Loc: New Jersey
Thanks Howard. I appreciate your help.
Top
#40808 - 2003-05-28 12:45 AM Re: check if a service is running
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Any other questions?
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40809 - 2003-05-28 12:47 AM Re: check if a service is running
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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 ]

Top
#40810 - 2003-05-28 12:49 AM Re: check if a service is running
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Long line police. You're busted DOC
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 557 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.051 seconds in which 0.017 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org