Is there something like cron jobs in IIS?
Asked Answered
B

4

13

I'm developing an ASP.NET web application. There is some functionality I need to trigger every 10 minutes. So currently I'm considering a 'scheduled task' or to create a Windows service to call the URL.

But I remember once I did the same thing in a PHP web hosting space, using a cron job.

So is there anything like cron jobs in IIS?

Note: I'm not expecting to use 3rd-party online scheduler services.

Barca answered 3/4, 2014 at 2:25 Comment(0)
S
7

Apples and oranges. cron is a Unix service. IIS is a web server. Just as cron is not included in Apache or nginx, there is no sense in which IIS contains a scheduler. However, you can use schtasks.exe in a similar way you can use cron on Unix.

You might find more ideas in this question.

Solanaceous answered 3/4, 2014 at 2:30 Comment(0)
S
3

I agree with @Amadan - apples and oranges.

However, I would tend toward writing a custom Windows service for this purpose rather than using Windows Task Scheduler. Another SO question speaks to pros & cons of each option.

While Windows services are straightforward(-enough) to develop, you might consider the open-source Topshelf framework to ease some of the development and deployment quirks that typically come with them.

And to be fair, remember that cron is not part of PHP of course, just a tool that may be available to you in *nix PHP hosting environments: ASP.NET is no different really; the question is what cron-like tool is available to you in your ASP.NET environment that meets your requirements and you prefer.

Swick answered 3/4, 2014 at 2:51 Comment(2)
Agreed, a custom web service is a better solution (and is listed in the question I linked). But schtasks.exe is the closest thing to cron in Windows, which answers the literal question OP asked.Solanaceous
@Amadan: Absolutely. Except for possibly needing to run tasks more frequently (more than once a minute) in the future, this is really a TMTOWTDI thing IMO.Swick
D
1

You may want to take a look at the Revalee open source project.

You can use it to schedule web callbacks at specific times. In your case, you could schedule a web callback (10 minutes in the future). When your application receives the callback, it can schedule the next 10 minute callback. When your ASP.NET application launches for the very first time, then you would schedule the first web callback. Since you web application is being called back you do not need to worry about IIS unloading your web application (which it, of course, will).

For example using Revalee, you might do the following:

  1. Register a future (10 minutes from now) callback when your application launches via the ScheduleTenMinuteCallback() method (see below).

    private DateTimeOffet? previousCallbackTime = null;
    
    private void ScheduleTenMinuteCallback()
    {
        // Schedule your callback 10 minutes from now
        DateTimeOffset callbackTime = DateTimeOffset.Now.AddMinutes(10.0);
    
        // Your web service's Uri
        Uri callbackUrl = new Uri("http://yourwebapp.com/ScheduledCallback.aspx");
    
        // Register the callback request with the Revalee service
        RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
    
        previousCallbackTime = callbackTime;
    }
    
  2. When the web scheduled task activates and calls your application back, you perform whatever action you need to do every 10 minutes and you schedule the next callback too. You do this by adding the following method call (CallbackMonitor()) to your ScheduledCallback.aspx page handler.

    private void CallbackMonitor()
    {
        if (!previousCallbackTime.HasValue 
            || previousCallbackTime.Value <= DateTimeOffset.Now.AddMinutes(-10.0))
        {
            // Perform your "10 minutes have elapsed" action
    
            // ...do your work here...
    
            // Schedule subsequent 10 minute callback
            ScheduleTenMinuteCallback();
        }
    }
    

Your point about not using '3rd party online scheduler services' is understood. The Revalee Service is not an external 3rd party online scheduler service, but instead a service (a Windows Service, more specifically) that you install and control fully on your own network. It resides and runs on a server of your own choosing, most likely behind your firewall, where it can receive callback registration requests from your web application on IIS. (It can, of course, be installed on the IIS web server if necessary.)

I hope this helps.

Disclaimer: I was one of the developers involved with the Revalee project. To be clear, however, Revalee is free, open source software. The source code is available on GitHub.

Diogenes answered 3/4, 2014 at 14:11 Comment(2)
-1: I assume that the OP wants a regular background task, which doesn't belong into a ASP.Net application at all.Fernand
If the 'run every 10 minutes' task is long running task, then I completely agree with you--an ASP.NET application is not the place to execute such; if, however, the scheduled task is meant to be a discrete transactional action (say, for instance, updating a database value or sending an automated email message), then Revalee can help make this a reality without requiring a cumbersome backend process to be setup (think: regular nightly job that combs through all records in a database table). My answer assumes that the OP's scheduled task is meant to be a discrete action.Benis
C
0

For the new readers, now you can easily create a background cron job from the .Net or .Net Core App itself using QuartZ Library. Instead of creating a task and windows service.

Chambray answered 5/8, 2021 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.