Friday, March 18, 2011

Use scheduled task to start / stop Windows Service

Windows doesn't offer to schedule start / stop of a Windows Service in a range of a time. But you can use this tips to do this.
Create 2 batch files named 'startMyService.bat' and 'stopMyService.bat' with the following content

# 'startMyService.bat'
# batch file to start my service
@echo off
net start "my Windows Service name"

# 'stopMyService.bat'
# batch file to stop my service
@echo off
net stop "my Windows Service name"

Next if you want for example that your service runs between 8h00AM and 5h00PM you can schedule the execution of 'startMyService.bat' at 8h00AM and schedule the execution of 'stopMyService.bat' at 5h00PM.
To schedule the execution of a program, go to 'Start' > 'Settings' > 'Control Panel' > 'Scheduled Tasks' > 'Add Scheduled Task' and follow the wizard.

No comments: