Solved

create batch file to stop and restart application and services

Posted on 2007-12-05
6
6,550 Views
Last Modified: 2010-08-05
Hi all,

I am not very good wioth DOS commands so here it is...

I need to create a bacth file that will perform the following actions in order

1.) Terminate an application such as MyApp.exe
2.) Stop MSSQL server
3.) Stop MSSQL Agent
4.) Wait 5 minutes
5.) Start MSSQL Server
6.) Start MSSQL Agent
7.0 Start MyApp.exe

Anyone have the necessary code to do this?
0
Comment
Question by:DLockwood
6 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 250 total points
ID: 20414449
I don't have the exact service names at hand at the moment, but you can retrieve them in a command window using
net start
which shows all running services.
The SQL Agent should be stopped before the SQL server, it's dependent on the server
Basically it would look like this (the service names still need to be corrected); sleep.exe is part of the W2k3 ResKit (see link below):

taskkill /im "MyApp.exe"
net stop "MSSQL Agent"
net stop "MSSQL Server"
sleep 300
net stop "MSSQL Server"
net stop "MSSQL Agent"
start "" "MyApp.exe"

Windows Server 2003 Resource Kit Tools
http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en
0
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 250 total points
ID: 20414453
Could try this:
@echo off
 

net stop MSSQLServer

net stop SQLServerAgent
 

echo Waiting for 5 minutes

ping -n 301 127.0.0.1 > NUL
 

net start MSSQLServer

net start SQLServerAgent
 

start myapp.exe

Open in new window

0
 
LVL 15

Expert Comment

by:bpmurray
ID: 20414541
Try something like the following:

          taskkill /IM MyApp.exe
          net stop "SQL Server"
          net stop "SQL Agent"
          ping 127.0.0.1 -n 301 -w 1000
          net start "SQL Agent"
          net start "SQL Server"
          MyApp.exe

The net stop/start commands may be slightly incorrect - if you do a "net start" from a command line, i.e. with no parameters, it'll list all the services available, including the correct names of the SQL services.



0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:DLockwood
ID: 20415205
oBdA and SteveGTR - THANK YOU SO MUCH!!!!

I split the points because:

oBdA - Your answer is right on except for 2 parts which were supplied by SteveGTR.
1.) The net stop and start commands
2.) Sleep doesn't work in windows 2003.

So, when I combined the 2 response from you guys it worked PERFECT!!

Thanks again!

DL
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20415226
Mine also was deficient as I didn't add the taskkill statement :)
0
 
LVL 83

Expert Comment

by:oBdA
ID: 20415286
D'oh to the "net stop" instead of the "net start"; too much copy and paste, and that's when I noted that the agent should be stopped before the server.
But I mentioned how to get the sleep.exe, which is quite useful (not to mention the rest of the tools), and I prefer it to using ping.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now