Configuring Windows services using Command Prompt
Configuring the properties of a Windows Service using command prompt and scripting it in a batch file is really simple and can save you from performing the same manual configuration again and again.
The sc utility comes handy to achieve this.
In the example below, we are going to configure a windows service to:
- Set the startup type as automatic.
- Run the service under a particular account
- Set the failure actions for the service and set the reset period.
- Define dependencies.
To give a more clear understanding, screenshots of the service properties UI is attached along with the scripts to set the configurations.
1. Set the startup type as automatic.
|sc config "Service1" start= auto|
Note here that although the Display name of the Service is myfirstservice, The actual Service name is Service1. We need to use the actual service name in the scripts to control the service properties.
2. Run the service under a particular account
|sc config "Service1" obj= mydomain\sidharth password= MyPassword|
3.Set the failure actions for the service and set the reset period.
|sc failure "Service1" actions= restart/180000/restart/180000/""/180000 reset= 86400|
The restart service times are in milliseconds and the reset fail count time is in seconds.
4. Define dependencies.
Our service depends on the SQL Server service, whose Actual Service name is MSSQLServer, To set this dependency, use the following command.
|sc config "Service1" depend= "MSSQLServer"|
Finally, to start the server use
|net start Service1|
We can also club all the command and save it as a batch file, it will look like this:
sc config "Service1" start= auto sc config "Service1" obj= mydomain\sidharth password= MyPassword sc failure "Service1" actions= restart/180000/restart/180000/""/180000 reset= 86400 sc config "Service1" depend= "MSSQLServer" net start Service1