How to restart windows service from SQL Job?

There could be many reasons for this requirement which, I am not going to discuss in this post. Here I will explain how you can do it.
Now, suppose you want to create an sql job that will restart window service, it means window service will be stoped first and then started again.

Following are the steps to create sql job that will do it.

1. Open Microsoft SQl Server Management Studio than click on the SQL Server Agent under the Sql server where you want to create sql job.
2. Right click on the jobs folder under SQL Server Agent and choose New Job option from the short cut menu.
3. In the New Job window General page, name your job.

Now you will have to create 2 job steps, first for stopping the window service and another one for starting it again, for that:

4. Click on the Steps in the "Select a page" section to open Steps page.
5. Click on the "New..." button on the Steps page to create new step.
6. In the new job step window, give name to your step in the "Step Name" field.
7. Choose "Operating system (CmdExec)" option from the dropdown for the job step type.
8. Enter following command in the "Command" field.

net stop "< Windows Service Name >"

like - net stop "Terminal Services" to stop terminal service.

9. Click on OK button to complete the job step creation process.
10. Now repeate steps 5 to 9 for creating another job step for starting the window service. But this time the command would be as follows:

net start "Terminal Services"

11. Now choose the first job step from the job step list and click on edit button, the job step will open in a window.
12. Click on the "Advance" page and choose second job step for the "On success action" and "On failure action" from the respective dropdown controls and click on OK button.

Reason: If service is already stop than this job step will fail so we will start the service otherwise if service is running than it will be stopped successfully so we will start it again.

13. Now go to the "Advance" page for the second job step and choose "Quit the job reporting success" for "On success action" and "Quit the job reporting failure" for "On the failure action" from the respective dropdown control.

There are other settings like Schedules, Alerts, Notifications, Targets for the
SQL Jobs that you can set according to your requirement, I am not going to discuss them in this post.

14. Now click on the OK button on the New Job window to complete the job creation process.

Now you are ready to run your newly created sql job to test it.