Run a scheduled task from web application

断了今生、忘了曾经 提交于 2020-03-18 07:00:29

问题


I am using the following code to execute a Windows Scheduled Task from my web application. Both the web application and the scheduled application runs on the same server.

var proc = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        FileName = @"C:\Windows\System32\schtasks.exe",
        Arguments = "/run /tn StartDataSync",
        UserName = "admin123",
        Password = passwd
    }
};
proc.Start();
proc.WaitForExit();

Now I would like to run the same scheduled application from a web application running on another machine in the same network domain. When I researched I found like I can specify the name or IP address of the remote computer I want to connect to in the /S system argument. So I tried the following code, but its not working.

var proc = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        FileName = @"C:\Windows\System32\schtasks.exe",
        Arguments = "/run /S 192.168.5.202 /tn StartDataSync",
        UserName = "admin123",
        Password = passwd
    }
};
proc.Start();
proc.WaitForExit();

回答1:


Try this answer ..according to this post

using TaskScheduler;

using (TaskService tasksrvc = new TaskService(server.Name, login, domain, password)
{
    Task task = tasksrvc.FindTask(taskName);
    task .Run();       
}

dll for task scheduler can be found here https://github.com/dahall/taskscheduler




回答2:


Have considered a different alternative to scheduling the execution of an EXE directly from your web application?

For example, scheduling tasks from within an ASP.NET project is possible using the Revalee open source project.

Revalee is a service that allows you to schedule web callbacks to your web application. In your case, you would schedule a callback that would synchronize your data at a specific time. Revalee works very well with tasks that are discrete transactional actions, like updating some database values or sending an automated email message (read: not long running). The code to perform your action would all reside within your app.

To use Revalee, you would:

  1. Install the Revalee Service, a Windows Service, on your server. The Windows Service is available in the source code (which you would compile yourself) or in a precompiled version available at the Revalee website.

  2. Use the Revalee client library in your Visual Studio project. (There is an MVC-specific version too.) The client library is available in the source code (which, again, you would compile yourself) or in a precompiled version available via NuGet.

  3. You would register a future callback when your code calls the ScheduleDataSynchronization() method (this example is assuming that you need your action to run 12 hours from now).

    private void ScheduleDataSynchronization()
    {
        DateTimeOffset callbackTime = DateTimeOffset.Now.AddHours(12.0);
    
        // The callback should be in 12 hours from now
        Uri callbackUrl = new Uri(string.Format("http://mywebapp.com/SyncData.aspx"));
    
        // Register the callback request with the Revalee service
        RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
    }
    
  4. When Revalee calls your application back, your app would perform whatever action you have coded it to do. You do this by adding the following method call (SynchronizeData()) to your SyncData.aspx page handler, à la:

    private void SynchronizeData()
    {
        // TODO Lookup the job information and execute the data synchronization
        // ...
    
        return;
    }
    

I hope this helps.

Note: The code example above uses a synchronous version of ScheduleCallback(), the Revalee client library also supports asynchronous calls à la:

RevaleeRegistrar.ScheduleCallbackAsync(callbackTime, callbackUrl);

In case it was not clear above, the Revalee Service is not an external 3rd party online scheduler service, but instead a Windows Service that you install and fully control on your own network. It resides and runs on a server of your own choosing, most likely your web server (but this is not a requirement), where it can receive callback registration requests from your ASP.NET application.

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.



来源:https://stackoverflow.com/questions/23440749/run-a-scheduled-task-from-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!