[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1088
  • Last Modified:

Batch File to Stop and Start a Service

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
shconsultancy
Asked:
shconsultancy
1 Solution
 
knightEknightCommented:
Try setting the scheduled task that runs the batch file needs to be run as the same user.
0
 
TG_TechCommented:
NET LOGON username password
0
 
questionCommented:
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
 
johnnash1180Commented:
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
 
shconsultancyAuthor Commented:
I tried that initially but I got the syntax wrong!!! Thanks for teh correction, it works just fine now.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now