_Chris_ Posted August 29, 2006 Posted August 29, 2006 Hi, i need a way of checking if a service is running. Is their a way of doing this as i want to include this check in an if statment for an installer. I cant seem to find anything on services in the help file? Am i just not looking hard enough?
amanda089 Posted August 29, 2006 Posted August 29, 2006 The only way I know of to do anything like this is to use CScript and capture the output to StdOut. I don't have any code examples, but a search of these forums should yield at least a few results.
MHz Posted August 29, 2006 Posted August 29, 2006 Try this for an idea. Services have been covered in great amounts on the forum. Search is your friend. $pid = Run('sc query dhcp', '', @SW_HIDE, 2) Global $data Do $data &= StdOutRead($pid) Until @error If StringInStr($data, 'running') Then MsgBox(0, 'Running', $data) Else MsgBox(0, 'Not Running', $data) EndIf
_Chris_ Posted August 29, 2006 Author Posted August 29, 2006 Thanks guys thats briliant. WHat i need to be able to do is query whether an SQL service is running (MSSQL$TT). I want to use the status of the service in an if statment, e.g. if state = running then ... else ... Any ideas??
rmarino Posted August 29, 2006 Posted August 29, 2006 _Chris_ said: Thanks guys thats briliant. WHat i need to be able to do is query whether an SQL service is running (MSSQL$TT). I want to use the status of the service in an if statment, e.g. if state = running then ... else ...Any ideas??Just query for sql. SC is a command line tool to see what the installed services are doing. You can also use a vbscript with WMI to do this.Here's a function that can be used to do this..........Function ProcList Dim objWMIService, objProcess, colProcess Dim strComputer, strList, fso, f Set fso = createobject("Scripting.FileSystemObject") Set f = fso.createTextFile("c:\process.txt", True) strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colProcess = objWMIService.ExecQuery _ ("Select * from Win32_Process") For Each objProcess in colProcess strList = strList & _ objProcess.Name & VbCrLf Next f.writeLine strList f.closeEnd Function
_Chris_ Posted August 29, 2006 Author Posted August 29, 2006 Thanks for your reply, im sorry im sure it works and all but i cant seem to figure out what i am going to do with that code!! Im still new to autoit and love it so i am really keen to learn more so i would appriciate some more help!!
MHz Posted August 29, 2006 Posted August 29, 2006 Thanks for the PMs. I will show the breakdown of handling the strings returned so you can use it if preferable. $pid = Run('sc query dhcp', '', @SW_HIDE, 2) Global $data Do $data &= StdOutRead($pid) Until @error $data = StringSplit($data, @CRLF, 1) If Not @error Then For $i = 1 To $data[0] ; Strip all whitespace $data[$i] = StringStripWS($data[$i], 8) If $data[$i] = '' Then ; Skip empty lines ContinueLoop ElseIf StringLeft($data[$i], 5) <> 'STATE' Then ; Skip lines that include no state ContinueLoop Else ; Remove "STATE:" and trim integer code $data[$i] = StringTrimLeft(StringReplace($data[$i], 'STATE:', ''), 1) EndIf ; Show compare to RUNNING and show result if True If $data[$i] = 'RUNNING' Then MsgBox(0, '', $data[$i]) EndIf Next EndIf Yeah, as rmarino shows, WMI is available also.
GaryFrost Posted August 29, 2006 Posted August 29, 2006 (edited) many ways: $service = "dhcp" MsgBox(0, $service, _RetrieveServiceState(@ComputerName, $service)) Func _RetrieveServiceState($s_Machine, $s_ServiceName) Local Const $wbemFlagReturnImmediately = 0x10 Local Const $wbemFlagForwardOnly = 0x20 Local $colItems = "", $objItem Local $objWMIService = ObjGet("winmgmts:\\" & $s_Machine & "\root\CIMV2") If @error Then MsgBox(16, "_RetrieveServiceState", "ObjGet Error: winmgmts") Return EndIf $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Name = '" & $s_ServiceName & "'", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If @error Then MsgBox(16, "_RetrieveServiceState", "ExecQuery Error: SELECT * FROM Win32_Service") Return EndIf If IsObj($colItems) Then For $objItem In $colItems Return $objItem.State Next EndIf EndFunc ;==>_RetrieveServiceState Edited August 29, 2006 by gafrost SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
ndaru Posted August 29, 2010 Posted August 29, 2010 On 8/29/2006 at 10:27 AM, 'MHz said: Try this for an idea. Services have been covered in great amounts on the forum. Search is your friend. $pid = Run('sc query dhcp', '', @SW_HIDE, 2) Global $data Do $data &= StdOutRead($pid) Until @error If StringInStr($data, 'running') Then MsgBox(0, 'Running', $data) Else MsgBox(0, 'Not Running', $data) EndIf Hi, Thank you for the script. I run 2 MySQL services on Windows XP, and ProcessExists can't tell which mysqld-nt.exe is running. Your script is exactly what I'm looking for.
BigGuy Posted December 3, 2010 Posted December 3, 2010 Greetings!, I am relatively new to AuoIT so hope this doesn't sound like dumb question. I have below script and it works fine when I run once but I want it to run continuously to monitor the service and alert me when service goes down. The script also starts the service back up. Again a very noob question but how do I run this script in a continuous loop. Thanks. Code Below: ------------------------------------------------------------------------- #include <smtp.au3> $GmailUser = "blah1@gmail.com" $GmailPass = "12345678" $ToEmail = "blah2@gmail.com" $pc = @ComputerName $service = "PeerDistSvc";BranchCache service $pid = Run('sc query PeerDistSvc', '', @SW_HIDE, 2) Global $data Do $data &= StdoutRead($pid) Until @error If StringInStr($data, 'running') Then Else RunWait(@ComSpec & " /c " & "sc \\" & $pc & " start " & $service, "", @SW_HIDE) _INetSmtpMailCom("smtp.gmail.com", @ComputerName, $ToEmail, $ToEmail, $GmailUser, $GmailPass, "Service " & $service & " is down", "Service is down") EndIf
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now