Results 1 to 5 of 5

Thread: Start and Stop Services in a single batch file?

  1. #1
    Join Date
    Aug 2006
    Posts
    253

    Start and Stop Services in a single batch file?

    Hello Friends,
    I was wondering, that is it possible to start or stop a service using a single batch file.?? Can I use the "if" type structure to do the both services in one batch file.?? I am very new to this batch files, so hoping that someone can help me out.! Please help me as soon as possible.!

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Start and Stop Services in a single batch file?

    You can create the batch files to schedule all sorts of things from Defragging drives, to performing backups, stopping services on servers, etc. The batch files are made like the DOS commands. In other words, you can say that the
    batch file is made of the exact same commands. Here is an example :
    Code:
    h:
    cd\retros~1
    copy *.* c:\retrocpy /V/Y
    You will have to open the Notepad which is a simple text editor. Type the commands that has been mentioned above into Notepad and then save the code. Also make sure that while saving the text file you will have to save it as .bat. You can do this by selecting Type as the 'All Files'. Hope that you will get some needful help from this post.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Start and Stop Services in a single batch file?

    I think that you can use the following code for Start and Stop Services in a single batch file. You will have to create a batch file as mentioned by the 'opaper'. The following coding you will have to save in the Notepad and then save it as Batch file. Here is the coding
    Code:
    IF "service"==stopped "NET START 'service'"
    
    IF "service"==started "NET STOP 'service'"

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Start and Stop Services in a single batch file?

    Instead of using the
    Code:
    IF "service"==stopped "NET START 'service'"
    IF "service"==started "NET STOP 'service'"
    you can also use the following
    Code:
    NET START "service" || NET STOP "service"
    The symbol "||" is used when you have to redirect to run the second command if the first fails. So there is no major difference in between the two coding.

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Start and Stop Services in a single batch file?

    You can also use the following code.
    Code:
    sc query service | findstr /i running | if "%errorlevel%"=="0" (sc stop service) else (sc start service)
    Now when the %errorlevel% is not 0, it means that the search string wasn't found in the output of the command and that doesn't mean that the command failed. Many think that when the %errorlevel% is not 0 means that there is some error in the coding so the command has failed. I would like to tell you that this is not the fact.

Similar Threads

  1. Replies: 1
    Last Post: 13-01-2012, 07:05 AM
  2. How to Start and Stop Gnome GUI Services
    By Lilija in forum Operating Systems
    Replies: 4
    Last Post: 12-11-2010, 07:30 AM
  3. Replies: 3
    Last Post: 07-07-2009, 08:27 AM
  4. Replies: 5
    Last Post: 24-04-2009, 04:45 AM
  5. Cannot Start / Stop services in Windows Server 2000 SP4
    By WarRen! in forum Window 2000 Help
    Replies: 5
    Last Post: 23-08-2005, 12:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,444,483.44979 seconds with 17 queries