我有什么选择来运行计划任务
我已经编写了一个修复程序,用于在我的网络服务器上运行预定作业,到目前为止该修复程序运行良好。
我需要在 IIS 7 机器上每 x 分钟运行一次作业。为了实现这一点,我创建了一个网页 (runcronjob.aspx),其中包含我想要运行的所有逻辑、检查数据库中的作业时间、上次运行时间、发送电子邮件、查询活动目录等。然后我使用了任务计划程序在我的工作站(不是网络服务器)上调用我制作的页面。我不再拥有始终位于网络上的专用工作站,因此我想将计划任务移至 Web 服务器,这就是我遇到的问题。
Web 服务器是运行 IIS 7 的 Windows 2008 x64 计算机。我尝试以与在本地计算机上相同的方式在 Web 服务器上创建计划任务,但它不起作用。计划任务以网页作为参数“http://mydomain.com/cron/runcronjob.aspx”调用IEXPLORE.EXE
。当 IE 7 在 Web 服务器上启动时,它会提示输入用户名和密码。我还尝试在服务器上本地运行 vbs 脚本:
runjob.vbs
Call LogEntry()
Sub LogEntry()
'Force the script to finish on an error.
On Error Resume Next
'Declare variables
Dim objRequest
Dim URL
Set objRequest = CreateObject("Microsoft.XMLHTTP")
'Put together the URL link appending the Variables.
URL = "http://mydomain.com/cron/runcronjob.aspx"
'Open the HTTP request and pass the URL to the objRequest object
objRequest.open "GET", URL , false
'Send the HTML Request
objRequest.Send
'Set the object to nothing
Set objRequest = Nothing
End Sub
我在这种情况下遇到的问题是我在网站上设置了表单身份验证。因此,当运行此脚本时,它会重定向到登录页面,而不是运行 runcronjob.aspx 上的代码。为了解决这个问题,我将此代码添加到登录页面的 Page_Load 部分的代码后面:
If Request.ServerVariables("LOCAL_ADDR") = Request.ServerVariables("REMOTE_ADDR") Then
FormsAuthentication.RedirectFromLoginPage("AD-ENT\accountToRunAs", False)
End If
登录页面执行该代码,但它永远不会重定向回 runcronjob.aspx 来运行该代码。
我开始没有主意了。
编辑: 我之所以用这种“hacky”方法来做到这一点,是因为我对网络服务器的访问非常有限,即使获得允许我在服务器上运行计划任务的权限,这也就像拔牙一样。话虽如此,创建控制台应用程序或要运行的 Windows 服务等选项可能会很困难。
I have hacked together a fix for running a scheduled job on my web server that has worked well enough up until now.
I had the need to run a job every x
minutes on my IIS 7 box. To accomplish this, I created a webpage (runcronjob.aspx) that contains all of the logic that I want to run, checks job times in the database, last run time, send emails, query active directory, etc. I then used Task Scheduler on my workstation (not the web server) to call the page I made. I no longer have a dedicated workstation that will always be on the network, so I'd like to move the scheduled task to the web server and this is where I'm getting stuck.
The web server is a Windows 2008 x64 machine running IIS 7. I tried to create the scheduled task on the web server the same way as I did on my local machine but it does not work. The scheduled task calls IEXPLORE.EXE
with the webpage as the parameter "http://mydomain.com/cron/runcronjob.aspx". When IE 7 launches on the web server, it prompts for a username and password. I also tried running a vbs script locally on the server:
runjob.vbs
Call LogEntry()
Sub LogEntry()
'Force the script to finish on an error.
On Error Resume Next
'Declare variables
Dim objRequest
Dim URL
Set objRequest = CreateObject("Microsoft.XMLHTTP")
'Put together the URL link appending the Variables.
URL = "http://mydomain.com/cron/runcronjob.aspx"
'Open the HTTP request and pass the URL to the objRequest object
objRequest.open "GET", URL , false
'Send the HTML Request
objRequest.Send
'Set the object to nothing
Set objRequest = Nothing
End Sub
The problem that I've run into with this scenario is that I have forms authentication setup on website. So when this script is ran, it redirects to the log in page instead of running the code on runcronjob.aspx. To get around this, I added this code to the code behind of the Page_Load secton of the login page:
If Request.ServerVariables("LOCAL_ADDR") = Request.ServerVariables("REMOTE_ADDR") Then
FormsAuthentication.RedirectFromLoginPage("AD-ENT\accountToRunAs", False)
End If
The login page executes the code, but it never redirects back to the runcronjob.aspx for that code to run.
I'm starting to run out of ideas.
EDIT:
The reason why I'm doing it in such a 'hacky' method is because I have very limited access to the web server and it was like pulling teeth even getting permission to let me run a scheduled task on the server. That being said, options like creating a console app or a Windows service to run could prove to be difficult.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您所做的只是数据库维护,那么您的计划任务根本不需要访问 Web 应用程序。
创建一个执行必要的数据库工作的控制台应用程序,并使用计划任务以指定的时间间隔运行控制台应用程序。
编辑
如果无法使用控制台应用程序,请使用计划任务来运行更新数据库的脚本。这里的要点是,无论您选择什么解决方案,您都应该直接访问数据库,而不是使用 Web 应用程序与数据库交互。
编辑
您还可以使用脚本执行其他操作,例如发送电子邮件、更新活动目录等。
Your scheduled task should not need to access the web application at all if all you're doing is database maintenance.
Create a console application that performs the necessary database work, and use a scheduled task to run the console application at specified intervals.
EDIT
If a console app is not a possibility, use a scheduled task to run a script which updates the database. The main point here is that whatever solution you choose, you should be accessing the database directly rather than using the web application to interface with the database.
EDIT
You can also use the script(s) to perform other actions, such as sending out emails, updating active directory, etc.
不幸的是,使用 IIS 而不是 Windows 服务进行调度需要这样的技巧...但我过去找到的最佳答案仍然是这个:
http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
至于处理身份验证 - 您可以在您的然后是IIS网站使用 wininet 到 SetCookie,使用身份验证服务的响应...这将使您的 GET 请求成功,而无需重定向到登录页面...,参考文献:
http://msdn.microsoft.com/en-us/library/aa385326(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/system.web.applicationservices.authenticationservice.aspx
It's unfortunate that it requires such a hack to use IIS as opposed to a Windows Service for scheduling...but the best answer I've found in the past is still this one:
http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
As for handling authentication - you could expose the AuthenticationService in your IIS website and then use wininet to SetCookie using the response from the authentication service...that would make your GET request succeed without redirection to the logon page.., References:
http://msdn.microsoft.com/en-us/library/aa385326(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/system.web.applicationservices.authenticationservice.aspx
您应该尝试将所有逻辑移至控制台应用程序并将其放入无限 while 循环中。
一旦它变得稳定,您应该将其重写为 Windows 服务并将其设置为随 Windows 启动(即无需登录)。
You should try to move all your logic to a console application and put it in an infinite while loop.
Once this becomes stable you should rewrite it as a windows service and set it start up with windows (i.e. without login).