1
votes

I have been running a batch file every 5 minutes through Windows Task scheduler and since there are quite a number of issues I faced like task scheduler going to hung state and not returning back to service- I have decided to use windows service primarily because I can invoke recovery action by monitoring the service through our monitoring infrastructure.

So, I have created a service to run that instead. The service was built and installed but the moment I start the service which invokes the batch file that is looped and doing a set of task, it keeps looping forever.

The batch file is something like this:

@echo off
:begin
cd c:\work\scripts\matm\
cscript //E:jscript c:\work\Scripts\matm\matm.js >> C:\work\Scripts\matm\matm.log;
cscript //E:vbscript c:\work\Scripts\matm\TruncateLog.vbs  
>>c:\work\Scripts\matm\TruncateLog.log;
del C:\work\Scripts\matm\Logs\myserver\matm.csv
timeout 600
goto begin

The batch script works perfectly when run from the command prompt and that is what I am expecting the service to invoke.

My thought is that the service gets into the loop as soon as we start it and never comes out of that.

I have defined the call to the batch file on this Onstart section as below

protected override void OnStart(string[] args)

My question is :-

a) How can I ensure that the service doesn't "start" running the batch file as soon as it starts? If my conception is wrong, how can I run the service every 5 minutes ?

b) How to stop the service? Or how can I stop the service if proc is a new instance of Process class that I have defined in the onstart() function.

Appreciate your help and feedback.

Regards, Sash

1

1 Answers

0
votes

Write a custom wrapper console application in C# that contains your error recovery logic.

Use the Windows Task Scheduler to invoke your wrapper application on a regular interval. You can configure it to only start the job if it is not already running. You can also make it kill the existing job.

No need to use a Windows Service. They are complicated.


Given that a Service might help you solve the problem here's what I'd do: In the OnStart method start a timer that ticks every 10min and starts your script. Or, start a thread that sleeps for 10min between calls to the script.