//TODONT: Use a Windows Service just to run a scheduled process

A common requirement in business application is a scheduled process - call a webservice, process the data, and FTP the results to a business partner, for instance. Developers kick around possible solutions - BizTalk's overkill, DTS won't handle it well, what to do?

Invariably, someone suggests a Windows Service with a timer. Just as invariably, when you try to talk them out of it, they'll condescendingly tell you that Windows Services are easy to write in .NET. "Trust me, it's not hard - we'll write a simple service with a timer which will do a simple time check..."

A Windows Service is the wrong solution to scheduling one-off custom processes. The right solution for scheduling simple processes is the Windows Task Scheduler.

Let's look at what Windows Services are designed for. From the Windows Help documentation:

Service
A program, routine, or process that performs a specific system function to support other programs, particularly at a low (close to the hardware) level. When services are provided over a network, they can be published in Active Directory, facilitating service-centric administration and usage. Some examples of services are the Security Accounts Manager service, File Replication service, and Routing and Remote Access service.

Web servers qualify as services, databases qualify as services, and the above Windows system features qualify as services. The Task Scheduler qualifies as a service. Possibly, custom server applications may qualify as a service if it needs to run round the clock and is written with the greatest sobriety, responsibility, and respect. Your scheduled file FTP doesn't qualify as a service.

A Windows Service which runs a timer needs to run Automatically, which means it will start up before you can log onto the machine and churn away until the machine is shut down. If they hang or have a slow memory leak, they'll continue to be a drag on the system until the service is restarted (or they crash the machine). I can't tell how many times I've heard the following in production meetings: "The cause of the outage was the WhamoInhouse Service. The resolution was to restart the service." Or, worse, application servers need to be restarted regularly because they're hosting poorly written services, but the IT shop blames the operating system.

Windows Services do offer sophisticated Recovery actions (on first, second, and subsequent failures you can specify that a program to be executed, or that the service or computer be restarted), but they're geared towards hard failures of the executable rather than business process failures, and they don't respond to things like memory leaks or hanging network connections. They offer absolutely no scheduling, because services are designed to run all the time. On the other hand, Windows Scheduled Tasks have advanced scheduling features which can be accessed administratively. If the business requirements for my scheduled FTP task change two years down the road so the FTP should happen twice a day every Monday, Wednesday, and Friday, the change can be made by an administrator. The task schedule has been tested across leap years, daylight savings time, and clock changes; most custom timer routines I've seen haven't.

Scheduled console applications are easier to design, build, test, deploy, and install - especially compared to a professional service which can run reliably for weeks end. This wouldn't by itself be a compelling argument if a Windows Service were the right solution by other criteria, but since it's not, this is another nail in the coffin. Consider, then, that your extra time spent building a service is wasted development time. Maybe fun "I am a bigtime coder now" development time, but not of any business value.

Scheduled console applications are easier to run in an ad hoc manner when necessary. Let's say the webservice vendor calls three hours after our process ran to inform us that he had a data corruption problem which has been corrected and we should re-run our process. In the case of a Windows Service with a timer, we'd be scratching our heads; with our console application we can run it manually whenever we want.

Finally, running separate Windows Services with their own timers is just plain inefficient with server resources. Ignore for the second the fact that we're hogging CPU and memory space 24/7 for a process that runs occasionally; multiple independant timers which check the time every few seconds is dumb.

If I kept writing Windows Services with timers, eventually I'd start to think about writing a single host system with a timer. Then I'd want to add in some more advanced scheduling features; perhaps even some logging. Eventually, I'd have a Windows Service that handles scheduling of child processes, and if I devoted years to enhancements and testing, I'd eventually arrive at... the Windows Task Scheduler.

If you're writing a Windows Service that runs a timer, you should re-evaluate your solution.

Published Monday, October 24, 2005 3:42 PM by Jon Galloway
Filed under: ,

Comments

# re: //TODONT: Use a Windows Service just to run a scheduled process

Great comment!

I made the same mistake of building an app with a timer, but in practice, the Win Task scheduler is turning out to be a much better use of resources for the process I'm doing.

Monday, October 24, 2005 3:01 PM by Eric Newton

# Not quite a fair comparison?

I'm not so sure that you can make a blanket statement like "A Windows Service is the wrong solution to scheduling one-off custom processes" without a little more qualification. For example, you don't really address the granularity of control over scheduled timings between the two options. In Task Scheduler, by default, you can run tasks as often as daily. In the advanced settings, I suppose you can repeat the task every X minutes for 24 hours until the task "starts over" but then that's not the same as just saying "run every X minutes forever" which might actually be what I want. What if I want to run every 5 or 10 seconds (i.e., for near-real-time processing)? You seem to be out of luck with Scheduler.

Which leads me to my other point: you mention that any simple process should not be a service, but how do you define "simple?" A process may not "DO" much, but may have a requirement like "it should only kick off once every 10 seconds, and can run for anywhere from 0 to 30 seconds depending on what there is to process." Surely even if the process itself is not "complicated," a service might still be a useful choice as it could internally handle things like concurrency and performance throttling (by skipping a particular timer iteration when needed). Maybe this would exceed your definition of "simple," but then, that probably means that "simple" needs to be better qualified before completely writing off services as a valid approach...

BTW, there is a reason NOT to use a windows service that you didn't mention. I've seen people try to work around the fact that you cannot interact with the desktop from a windows service when in fact, they would have been better off just writing a winforms app running as a scheduled task, as you propose. I agree with your basic premise -- the approach should suit the requirement.

Monday, October 24, 2005 7:44 PM by Brian

# re: //TODONT: Use a Windows Service just to run a scheduled process

It's perfectly possible to write a windows service which installs, uninstalls, runs as a service, and runs in console mode all from the same executable. So this takes care of the head scratching.

Windows Services are great for always on, no desktop interaction, dependency on other services, recovery in case of hard failures, remote management via various APIs.

Tuesday, October 25, 2005 11:37 AM by Yves Reynhout

# re: //TODONT: Use a Windows Service just to run a scheduled process

Brian -
I'll agree - if you need your process to run more often than once a minute, a Service probably makes sense. At that point, your program is running pretty much continually, which I see as a Service.

I don't see your second point, though. There's no reason a console app kicked off by the Task Scheduler can't have an internal timer. The point is that Services offer zero support for scheduling, whereas the Scheduler offers at least a framework. Again, if you're talking seconds then you're not really talking about a scheduled process any more.

Tuesday, October 25, 2005 6:21 PM by Jon Galloway

# re: //TODONT: Use a Windows Service just to run a scheduled process

Yves -
I don't see how that solves anything. We still need to decide how to deploy the application, and what we use to schedule it. Should I build a multifunctioned app with a timer, internal scheduling, and support for service install just in case, then deploy it as a console app under the Scheduler? (scratches head)

The point is that we need to pick a system architecture. I agree with all the good things you mentioned about Services and would gladly use a Service for always on systems, but they don't make sense for occasional scheduled tasks.

Tuesday, October 25, 2005 6:26 PM by Jon Galloway

# re: //TODONT: Use a Windows Service just to run a scheduled process

I'll concede the obvious, that the Windows Task Scheduler is a service that allows scheduling of tasks, and it is generally better to use an existing service than to roll your own that does the same thing. But...

Windows Task Scheduler sucks. Just one example: http://girtby.net/archives/2005/08/03/scheduled-task-slippage-and-breakage/

Tuesday, October 25, 2005 10:00 PM by Steve Campbell

# re: //TODONT: Use a Windows Service just to run a scheduled process

Jon -

Why do I feel like this was directed at me? :)

Of course, I agree with you - Windows Services ARE significantly overkill for timed apps. The Task Scheduler is a much better solution - however, there are some very good reasons for using a service.

Services can be easily monitored using something like MOM to instantly alert admins if the service stops or hangs. If someone comes in and cancels or pauses your scheduled service, you won't know it right away. You MIGHT know it if you routinely monitor the OUTPUT of the app, and you see that it didn't run at it's required time.

That might be fine for a lot of scheduled processes... but if the process absolutely, positively has to run at a certain time, and we need to actively monitor it to ensure that it can, then writing a Windows Service might be a valid choice. At least until the Task Scheduler has the ability to monitor the status of every scheduled task, and escalate issues.

Thursday, November 10, 2005 3:35 PM by Bill Richardson

# re: //TODONT: Use a Windows Service just to run a scheduled process

Bill -
I don't know how many times I've had this discussion, and your name definitely wasn't on my mind when I wrote this.

Part of the reason I posted this was to hear some good counterpoints. Thanks for yours - that definitely makes a lot of sense.

Thursday, November 10, 2005 6:24 PM by Jon Galloway

# re: //TODONT: Use a Windows Service just to run a scheduled process

I have a Lotus Notes Agent (Java Agent to be specific) calling a .Net URL, which in turn do file download and processing.
Since I use time tested schedule functionality of Lotus Notes to trigger a URL, I do not have to worry about scheduling at .net side..

But I wonder whether this has any bad side effects..

Tuesday, June 27, 2006 12:52 PM by Jose Manayil

# Windows Services Or Task Scheduler? - Part I

Before I even start I’m sure all you know what I’m going to talk about. First let me explain what our

Monday, October 09, 2006 3:44 PM by DDITWeb

# re: //TODONT: Use a Windows Service just to run a scheduled process

I agree with you 100% on this one. If you need a Windows service, you'll know it, and it will be something much more involved than simply timing a job.

Console applications are much easier to write, and also *much* easier to debug than a Windows service.

The main reason I would default to a Windows service is if I needed to trap events, keep watch on something, or listen for incoming connections, but even for those there are solutions that I would pick over a Windows service. Especially in a SOA, scheduling console apps to interact with your services becomes very common, and a very elegant and simple design.

Friday, July 13, 2007 2:06 PM by Eric F Kaufman

# re: //TODONT: Use a Windows Service just to run a scheduled process

thank you very much, it is very helpful.

Wednesday, August 08, 2007 9:15 AM by Sinan

# re: //TODONT: Use a Windows Service just to run a scheduled process

I have schedule a task to run in win2000 every 1 minutes but i need to reduce the time every 30 seconds.

help required.

Saturday, November 10, 2007 5:37 AM by prasanna

# re: //TODONT: Use a Windows Service just to run a scheduled process

depends on the task. If there is no UI then use a service and control-m job to schedule it

Tuesday, February 05, 2008 10:23 AM by Zorg the Mighty Coder

# re: //TODONT: Use a Windows Service just to run a scheduled process

Googling around to find a possibility to use seconds in Scheduled Tasks, I found this thread.

I think there is definetely a need for seconds in Scheduled Tasks. Ref. *NIX crontab.

Case: Two simulation cases. Each must run every two minutes but they can't be run simultaneously. Then you have something running every minute. Each calculation case uses between 20 and 30 seconds to finish. Never more than 40 secs.

Problem: When are you going to run the daily db housekeeping etc. which should be run when the simulation cases are not running?

I had to use a hack with sleep (which is not included in Win2k, by the way). This is still task scheduling, not continous or real-time operation. And the OS scheduler should handle that - one shouldn't have to write services to do it :)

Best regards,

Lars

Sunday, March 16, 2008 4:24 AM by Lars

# re: //TODONT: Use a Windows Service just to run a scheduled process

There are also some third party solutions to run apps AS services, firedaemon being one of them.  You get the best of both worlds then.

Wednesday, April 02, 2008 10:39 AM by Tony

# Using a Windows Service just to run a scheduled process is Wrong!

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Thursday, May 08, 2008 2:31 PM by DotNetKicks.com

# Windows Services vs Scheduled Tasks

Windows Services vs Scheduled Tasks

Friday, May 09, 2008 3:58 PM by moggoly.me.uk

# Do Not Use Windows Services as a Cron

Do Not Use Windows Services as a Cron

Sunday, May 11, 2008 10:55 PM by Tech Guru

# re: //TODONT: Use a Windows Service just to run a scheduled process

Good post.  I was getting dangerously close to writing my own Windows Service to handle the scheduling of multiple jobs.  I will definitely re-think that approach.  FireDaemon looks interesting.  Thanks for that suggestion, Tony.

Tuesday, May 13, 2008 10:44 AM by Michael Paladino

# re: //TODONT: Use a Windows Service just to run a scheduled process

I like the "Task Scheduler" in Vista and Windows Server 2008:  it's easy to schedule something to run every 5 minutes indefinitely or do just anything I need.

I'm a long term Unix user,  and I always felt that cron was a lot better than the "Task Scheduler" in Windows XP.  These days I'd like to see a GUI for cron that's half as good as Vista's Task Scheduler.

Tuesday, May 13, 2008 3:41 PM by Paul Houle

# Finds of the Week - May 31, 2008 » Chinh Do

Pingback from  Finds of the Week - May 31, 2008 » Chinh Do

Sunday, June 01, 2008 10:51 PM by Finds of the Week - May 31, 2008 » Chinh Do

# re: //TODONT: Use a Windows Service just to run a scheduled process

I see a couple of issues with your blanket assumptions and solution. Whih a console app run as a schedulked task, that app has to run under the account of a role or individual. Then you have issues with passwords...

If you have an unhandled exception, the app hangs until someone pcanywheres to the box clears out the error. On a ntservice it will be cleared, logged and restarted. I do nto have to actually pcanywhere to do administrative tasks. I just attach service manager to the servers in question. Say you have written an application to post data to a webservice or save off data from your webserver to a different server and you want it to run every 5 minutes, and you have numerous servers for this to run on. You are MUCH better off writing an NT service to mange the transactions just for the reasons above. If you have 10k a day hitting each server, the console app from a scheduled task is a nightmare from a maintenance perspective. In my company of over 3k in IT, we have both running, we get that type of traffic, and our plan is to replace EVERY schedule task that is doing 'service' type work.

In my experience people who create a scheduled task and console app are the same people trying to get excel installed on the webservers too.

If  I had it running once a day fine, your solution woudl be fine. You will still have issues with DR watson, errors, potential security problems(It is much easier to change the code of a console executable on a managed server than it is a NT service) If I uninstall and reinstall a service that will show up in the logs. Replace an exceutable, no one many notice.

If your approach is the best way, why are anti-virus software managed as services? Why isn't indexing just a scheduled task that goes off every few minutes.

Wednesday, June 04, 2008 1:59 PM by Bob Bobberson

# re: //TODONT: Use a Windows Service just to run a scheduled process

Debugging nt services are not that bad, while I will admit it is easier to just hit the debug option in VS. In a NT service it isn't that hard. Create a batch file to uninstall the service, reinstall the new version. Set your break point, and attach to the process. You can then step through the code with no issues.

Wednesday, June 04, 2008 2:03 PM by Bob Boberson

# re: //TODONT: Use a Windows Service just to run a scheduled process

Nice article.  I almost went down the path of writing a windows service to run a couple of jobs...one once a month and another once a week.  Service approach definitely overkill and I think I'll go for a couple of simple console apps and the windows task scheduler now.

Friday, June 06, 2008 10:56 AM by robinsonpr

# re: //TODONT: Use a Windows Service just to run a scheduled process

but the problem of using console application and scheduler are: 1st, we need to login the system. 2nd, the console application might popup the DOS window.

Monday, June 09, 2008 3:19 PM by kenny

# re: //TODONT: Use a Windows Service just to run a scheduled process

Jon,

How do I configure the task scheduler for my application as a part of my setup/install program?

Tuesday, June 17, 2008 10:13 AM by Lilu

# re: //TODONT: Use a Windows Service just to run a scheduled process

Let me start be saying I'm all for easy, but the bottom line is Windows Schedule Tasks is poorly written code. I have three dramtically different environments all running the same little .exe, none of them are stable or reliable. Oh it will tell you it has run with no problems, and it's lying through it's teeth. I've tried launching to from a batch file - no good. I've tried 3600 different combinations of timers, multiple jobs, so on and such forth. It will run for a couple of hours or days, then fail. It has been the most frustrating thing I've dealt with in recent history.

So let me reiterate, if you rely on the task to run - DO NOT USE the task scheduler.

Thanks for letting me vent...

Monday, June 30, 2008 2:28 PM by Jeb buie

Leave a Comment

(required) 
(required) 
(optional)
(required)