-1

I just need to create a batch file which can check service every minute and once the service going to Stopped so the Window CMD will close.

More clear:

  1. Start service (do not close the CMD window)
  2. Check every 1 min the service status
  3. Once the service becomes to "Stopped" status close the CMD window
Merop4
  • 11
  • 4
  • Can you rephrase this as a question? – niallhaslam Jul 28 '15 at 10:35
  • possible duplicate of [Stop and Start a service via batch or cmd file?](http://stackoverflow.com/questions/133883/stop-and-start-a-service-via-batch-or-cmd-file) – wOxxOm Jul 28 '15 at 10:36
  • it's doesn't connected to my request ... – Merop4 Jul 28 '15 at 10:38
  • @niallhaslam i just need to create very small script that able to : start specific service ( , once the service will be "Stopped" so CMD window will be close , perhaps its complected script because the batch file needs check every 1 min what is the status . stooped or running , once it stopped , close the CMD window with Exit command . that it! – Merop4 Jul 28 '15 at 10:39
  • Can you explain a bit more about what you have tried so far and why that didn't work? – niallhaslam Jul 28 '15 at 10:43
  • Again, this what i have created : @echo off net start "XXXX" :1 timeout 10 for /f "tokens=4" %%a in ('sc XXXX ^| findstr STATE') do ( echo %%a if %%a==RUNNING echo The Service is up if %%a==RUNNING goto :1 ) exit as you see , the script started specific service and after X time the script should check what is the status of service , as long as the script is running don't close CMD window (Batch file ) once the script will change status to "Stopped" close the CMD window (Batch file ) – Merop4 Jul 28 '15 at 10:50

1 Answers1

1

the SC (service control) command, it gives you a lot more options.

DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. http://www.robvanderwoude.com/sc.php

USAGE: sc [command] [service name] ...

  The option <server> has the form "\\ServerName"
  Further help on commands can be obtained by typing: "sc [command]"
  Commands:
    query-----------Queries the status for a service, or
                    enumerates the status for types of services.
    queryex---------Queries the extended status for a service, or
                    enumerates the status for types of services.
    start-----------Starts a service.
    pause-----------Sends a PAUSE control request to a service.
    interrogate-----Sends an INTERROGATE control request to a service.
    continue--------Sends a CONTINUE control request to a service.
    stop------------Sends a STOP request to a service.
    config----------Changes the configuration of a service (persistant).
    description-----Changes the description of a service.
    failure---------Changes the actions taken by a service upon failure.
    qc--------------Queries the configuration information for a service.
    qdescription----Queries the description for a service.
    qfailure--------Queries the actions taken by a service upon failure.
    delete----------Deletes a service (from the registry).
    create----------Creates a service. (adds it to the registry).
    control---------Sends a control to a service.
    sdshow----------Displays a service's security descriptor.
    sdset-----------Sets a service's security descriptor.
    GetDisplayName--Gets the DisplayName for a service.
    GetKeyName------Gets the ServiceKeyName for a service.
    EnumDepend------Enumerates Service Dependencies.

  The following commands don't require a service name:
  sc <server> <command> <option>
    boot------------(ok | bad) Indicates whether the last boot should
                    be saved as the last-known-good boot configuration
    Lock------------Locks the Service Database
    QueryLock-------Queries the LockStatus for the SCManager Database

EXAMPLE: sc start MyService

SDM
  • 26
  • 3
  • Specifically, `for /f "tokens=1,4" %%a in ('sc query SERVICENAME') do if %%a == STATE set STATE=%%b` will set `STATE` to `RUNNING` or `STOPPED` (and occasionally some others). You can then build this into an appropriate loop. – TripeHound Jul 28 '15 at 10:44
  • Hi i don't want stop service , the service know alone stop itself after some minute , i just need that CMD file stay open while service is "Running " once it become to "Stopped" Close CMD window – Merop4 Jul 28 '15 at 10:45
  • I have created this one : @echo off net start "XXXX" :1 timeout 10 for /f "tokens=4" %%a in ('sc XXXX ^| findstr STATE') do ( echo %%a if %%a==RUNNING echo The Service is up if %%a==RUNNING goto :1 ) exit really start the service but not working well – Merop4 Jul 28 '15 at 10:46
  • 1
    @Merop4 The command I gave doesn't stop or start the service, just acquires its status so you can build that into your script. If you've got a script that uses either this or your variant using `findstr` then edit that script into your question (so we can see it clearly) and specify **in what way** it doesn't do what you want. – TripeHound Jul 28 '15 at 11:04