1

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

Sash Sheen
  • 105
  • 2
  • 14

1 Answers1

0

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.

usr
  • 168,620
  • 35
  • 240
  • 369
  • I want to do away with task schedulers. It also exposes my code to everyone.Is there any alternative? – Sash Sheen Dec 05 '13 at 09:08
  • The Task Scheduler does not just get hung. It has been used on millions of servers for 10 years. Maybe your script gets hung (in which case invoking it in a different way does not help). Writing a service does nothing for you (if you disagree - what does it help you?). Anyway, I'm unclear on your subquestions a) and b). But they go away if you don't use a service but a normal console application.; Also your script seems to have an infinite loop that runs every 10min. Can't you just start it once using the Task Scheduler on Windows boot and it will run forever? Why doesn't that work? – usr Dec 05 '13 at 09:19
  • Okay, let me elaborate. The scripts are running on task schedulers for quite sometime now but for the task scheduler to run, I need to have elevated permissions for the account. Now, per the new group policy that gets enforced on the boxes, I have seen the user getting removed from the elevated permissions group(I always add it to the admin group)and the task scheduler sits there doing nothing.Using task scheduler, I run the batch file every 10 min so there is no infinite loop. – Sash Sheen Dec 05 '13 at 09:34
  • I dont know if building a service can help that dependency go. Would you know if selecting Local System Account helps as there is no specification of a user account there. And I have modified the batch file so that the script continues to run endlessly every 10 minutes once the service is started because I dont think there is a way to put a wait period time on the service. Going back to the second subquestion, the scripts are jscripts and vbscripts. I dont want it to be exposed to others. Creating a service helps in a small bit to actually hide what batch file is being run. Thoughts? – Sash Sheen Dec 05 '13 at 09:37
  • I'm not strong on Windows security concepts. Is it not possible to make the scheduled task use Local System? (http://stackoverflow.com/questions/6477807/how-to-create-a-scheduled-task-under-system-user-account-with-schtasks) 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. The OnStart method is supposed to return quickly as far as I know because there's a timeout on service startup. – usr Dec 05 '13 at 09:42
  • One seldom wants to use the Local System account for anything. It should be avoided. – David Heffernan Dec 05 '13 at 09:51