In most cases there is not much you need to do with a Windows service once it is running. However, certain applications may install a custom service in Windows that can fail for one reason or another. In the case outlined below, I am working with a custom service that handles scheduling integration maps between Dynamics GP and another database. After doing some research I found that this service has some known issues which cause it to hang up. The suggested “solution” for this was to created a scheduled task within Windows to periodically stop and then start the service.
The steps outlined below will walk you through the process of creating a batch file to handle stopping and starting the service as well as creating a scheduled task to call the batch file.
In order to create the batch file the first thing you’ll need to do is launch Notepad. The basic command for calling a service stop and start are shown below:
NET STOP Service Name
NET START Service Name
In order to locate the service name, simply open up the Services window, locate the service and right-click to select Properties.
In this example I am referencing the eOne SmartConnect Service. On the service Properties window copy the Service name: value. In this case eOne.SmartConnect.WindowsService.exe
Now go back to Notepad and enter the appropriate commands into the file. The example below illustrates the correct command to stop and then start the eOne Smart Connect Service shown above.
NET STOP eOne.SmartConnect.WindowsService.exe
NET START eOne.SmartConnect.WindowsService.exe
echo %date% - Service Restarted Successfully >>"C:\eOne Service Restart\restart.log"
I have also included an additional command to write to a predefined log file once the batch file has been executed. The
echo command is used to write text to the screen or a file based on how you call it. In the example above, the batch file will write the current date along with “Service Restarted Successfully” to a log file in the C:\eOne Service Restart directory.
The directory must exist in order for the log file to be updated. However, for the initial execution, the log file does not need to exist and it will be created once the command has been called.
Now that the batch file has been created the next step is to use the Windows Task Scheduler to create a scheduled task to call the batch file. To do this, launch Task Scheduler (Administrative Tools >> Task Scheduler).
On the right-hand side of the screen click the option for Create Basic Task…
On the initial screen, titled Create Basic Task, all you need to do is give the task a name. In the example below I have named the task eOne Service Restart. Once a name has been entered, click Next>
The next screen is where you will begin configuring the Trigger. The Trigger is basically the schedule that you want to set. In this example I want the scheduler to kick off every day at 1:00 AM. If you’re familiar with creating schedules in other applications such as SQL Server then this part should be pretty simple.
By default the radio button next to Daily will be flagged. Since I’m creating a daily schedule, there is no need to change this. Once you have selected the desired starting point click Next>
The next step is to configure the actual time you would like the scheduler to run. As mentioned above, I want it to run at 1:00 AM daily. The screenshot below illustrates this.
Once you have configured the Start time click Next>
The next screen is where you will configure the Action to be taken. The three options here are to Start a program, Send an e-mail or Display a message. Since we want to call a batch file the option we will select here will be Start a program. This is also the default option.
Once you have selected the desired Action item click Next>
The next step will be to provide the path to the batch file that was created previously. I would highly recommend putting the batch file somewhere on the C:\ drive, preferably in it’s own directory.
Click the Browse button next to Program/script and locate the batch file. The example below shows where I have selected my batch file from the eOne Service Restart directory.
Once you have referenced the batch file click Next>
For the final step, before clicking Finish, make sure to check the box next to Open the properties dialog for this task when I click Finish. This will allow you to make a few additional, yet important, changes to the task before saving it.
Once the checkbox has been selected click Finish.
The options that you will select in the dialog window mostly depend on the usage of the server where the task is being configured. For the most part you only need to make changes to the General tab as the other tabs only show the configuration settings from the previous steps outlined above.
In most cases I would recommend changing the User account that calls the task as well as selecting the radio button next to Run whether user is logged in or not and selecting the check box next to Run with highest privileges.
Though I did not change the account in the example above, typically it is best to set a service account or some other domain level account where the password will not expire. Otherwise you run the risk of causing the scheduled task to fail if you change your password and do not go back and update the scheduler.
Once you have made the desired configuration changes click OK to save the scheduled task.
The batch file will now be executed at the specified time. If you took the steps to include the log file then you can check the file periodically to make sure the scheduler is running correctly.