Solved

Batch File to Stop and Start a Service

Posted on 2009-05-11
5
1,045 Views
Last Modified: 2012-05-06
Experts

I have a batch file that will need to be scheduled at 5:00 in the morning. This will stop and start a particular service.
When I run the command the service stops but will not start again. I have narrowed teh problem down to teh Log On option within the properties of the service.
Rather than using Local System account I am using Log On As with a User ID and password.
How can I fit this into the batch file without changing it back to Local System?
My command line is:

Net Stop "Service"
Net Start "Service"

Cheers
0
Comment
Question by:shconsultancy
5 Comments
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
Try setting the scheduled task that runs the batch file needs to be run as the same user.
0
 
LVL 4

Accepted Solution

by:
TG_Tech earned 250 total points
Comment Utility
NET LOGON username password
0
 
LVL 6

Expert Comment

by:question
Comment Utility
well... use the command Net Stop "Service" && Net Start "Service"
often.. the command does not wait for the first step to complete before proceeding to next step hence altering it in format && it waits for first command to complete before moving further.

-?
0
 
LVL 7

Expert Comment

by:johnnash1180
Comment Utility
I believe that you are stopping the service and then immediately starting the service. It may take some time to stop the service. At that time service will be in stopping state. When the service is in stopping state, you cannot start it. Instead, you can wait for some time and then start the service. You can add delay by using "ping localhost -n <time in seconds>" command.

The code will be as follows:

net stop service
ping localhost -n 10
net start service

Change the 10 value to a suitable value.

The best method is getting status of service by sc command. To get the status of a service, you can use 'sc query <service name> | find "STATE" ' command.

John Nash
0
 
LVL 1

Author Closing Comment

by:shconsultancy
Comment Utility
I tried that initially but I got the syntax wrong!!! Thanks for teh correction, it works just fine now.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

12 Experts available now in Live!

Get 1:1 Help Now