Jump to content

how to see if a services is stopped or started


 Share

Recommended Posts

maybe you could use net start from a cmd prompt window...

n9mfk9,

I've noticed you checking this thread a few times since I posted that one line answer. Perhaps this post will be of more use to you than my first one:

;name of the service to test
$SeviceName = "Task Scheduler"

;send a list of the services that are running to a file
RunWait(@ComSpec & " /c net start > c:\temp\services.txt", "", @SW_HIDE)

;read that file
$FileText = FileRead("c:\temp\services.txt", FileGetSize("c:\temp\services.txt"))

;check for the service name
If StringInStr($FileText, $SeviceName) Then
    MsgBox(0, "AutoIt", "The sevice named " & $SeviceName & " is running.")
Else
    MsgBox(0, "AutoIt", "The sevice named " & $SeviceName & " is not running.")
EndIf
Read the help file for some error checking code that you might want to add for FileRead.

Edit: You did ask for "the best way".

The code above is not "the best way"

since you could use the beta release of AutoIt3

and avoid the use of that temp text file.

http://www.autoitscript.com/autoit3/files/beta/autoit/

...but the code above is a simple way to get you started.

Hope this helps.

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

what do you mean if i use the bata i can get around the temp

can you show me thanks beau

The simple way to do what you want done was shown in my posts above. It writes the output of the cmd prompt window (DOS window or console) to a temp file. In the example above, that temp file is named "c:\temp\services.txt". Then that simple code reads that temp file into a variable and looks for the string of interest.

With the Beta version of AutoIt3, you should be able to skip writing to that temp file. The Beta version can just read the output of the console without having to write it to the temp file and read it back again. I think that the function is named "StdoutRead" - but I do not have the Beta version of AutoIt3 and I cannot help rewrite my "simple code" shown above to skip the steps that use the temp file.

Maybe someone else in the forum will post better code with the StdoutRead method...

...or you can read more about it in this post:

http://www.autoitscript.com/forum/index.ph...ndpost&p=122457

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

hi all what would be the best way to have a script check an see if a service is stopped or started

an the have the script go from there thanks for any ideas

If you have sc.exe in your system directory:

sc query "servicename"

You can pipe to a file and read the state of the service.

Or StdReadOut (AutoIt Beta):

$servicename = 'wzcsvc'

$stdout = Run('sc.exe query ' & $servicename, '', @SW_HIDE, 2)
$data = _StdOut($stdout)

Func _StdOut($stdout)
    Local $data
    While 1
        $data = StdOutRead($stdout)
        If @error Then Return 'unknown'
        If $data Then
            $data = StringStripWS($data, 4)
            Select
                Case StringInStr($data, 'STATE : 1')
                    Return '1'
                Case StringInStr($data, 'STATE : 2')
                    Return '2'
                Case StringInStr($data, 'STATE : 3')
                    Return '3'
                Case StringInStr($data, 'STATE : 4')
                    Return 'Running'
                Case Else
                    Return 'unknown'
            EndSelect
        Else
            Sleep(100)
        EndIf
    WEnd
EndFunc

MsgBox(0, '', $data)

Just workout state 1, 2 or 3. I know 4 is running :P

Edit: But perhaps searching for the string Running etc, would be more correct as the numbers would be the state for service startup, hmm.

Or search for a WMI example for services mentioned before for beta users.

Edited by MHz
Link to comment
Share on other sites

  • 1 year later...

If you have sc.exe in your system directory:

sc query "servicename"

You can pipe to a file and read the state of the service.

Or StdReadOut (AutoIt Beta):

No mater How i try, I simply cant use this whole thing in a function.. i get errors.. basically i want to do this:

HotKeySet('{F5}', '_check')
func _check()
; check if its running....
EndFunc

Maybe someone can explain me how to use this recrusion loop?

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Based on Mhz's script, something roughly like this

HotKeySet('{F5}', '_check')
HotKeySet("{ESC}", "_Terminate")

$servicename = 'helpsvc'

while 1
    Sleep(100)
WEnd

Func _Terminate()
    Exit
EndFunc   ;==>_Terminate

Func _check()
    Local $data
    $stdout = Run('sc.exe query ' & $servicename, '', @SW_HIDE, 2)
    While 1
        $data = StdOutRead($stdout)
        If @error Then
            $state = 'unknown'
            ExitLoop
        EndIf
        If $data Then
            $data = StringStripWS($data, 4)
            Select
                Case StringInStr($data, 'STATE : 1')
                    $state = '1'
                Case StringInStr($data, 'STATE : 2')
                    $state = '2'
                Case StringInStr($data, 'STATE : 3')
                    $state = '3'
                Case StringInStr($data, 'STATE : 4')
                    $state = 'Running'
                Case Else
                    $state = 'unknown'
            EndSelect
            ExitLoop
        Else
            Sleep(100)
        EndIf
    WEnd
    MsgBox(262144, '', "Service " & $servicename & @CRLF & "State = " & $state & @CRLF & @CRLF & '"ESC" to exit after closing this window')
EndFunc
Link to comment
Share on other sites

Check this solution out:

$SecLogonState = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\seclogon", "Start")
If $SecLogonState = "4" Then ; 4 means disabled, 3 means manual, 2 means automatic, 1 means system (?), 0 means boot (?)oÝ÷ Ûú®¢×±ö«jÂ.׫²Ö§vØb²åÊ·­º¹ì¶µ«n²Ü©y©©ç$²'â¶Êîx§ç¢Úbq¬^N§Äj-×â©eË
+)à*ÞËayÊz'ì¢g¨è¬{^­ë-yØ.zË"Zh²Ö­¢«az¶¦¥«$²¢w­¡ìi¨§ëaÊÞr»¬í)àjëh×6HotKeySet('{F6}', '_check')

$servicename = 'MSSQLSERVER'
Global $state_
_main()
Func _main()
    While 1
   _check()
   If $state_ = 'Running' then ExitLoop
   WEnd
    MsgBox(262144, '', "Service " & $servicename & @CRLF & "State = " & $state_)

   _Terminate()
EndFunc 

Func _Terminate()
    Exit
EndFunc   ;==>_Terminate

Func _check()
    Local $data
    $stdout = Run('sc.exe query ' & $servicename, '', @SW_HIDE, 2)
    While 1
        $data = StdOutRead($stdout)
        If @error Then
            $state = 'unknown'
            ExitLoop
        EndIf
        If $data Then
            $data = StringStripWS($data, 4)
            Select
                Case StringInStr($data, 'STATE : 1')
                    $state = '1'
                Case StringInStr($data, 'STATE : 2')
                    $state = '2'
                Case StringInStr($data, 'STATE : 3')
                    $state = '3'
                Case StringInStr($data, 'STATE : 4')
                    $state = 'Running'
                Case Else
                    $state = 'unknown'
            EndSelect
            ExitLoop
        Else
            Sleep(100)
        EndIf
    WEnd
    $state_ = $state
EndFunc

while 1
    Sleep(100)
WEnd
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...