简体   繁体   English

Windows的Power-shell脚本调度不可靠

[英]windows scheduling of a power-shell script is unreliable

I'm attempting to run an automated powershell script all day, everyday. 我每天都在尝试运行自动化的Powershell脚本。 But I'm have problems getting it to run consistently and reliably, the script itself runs fine, it's getting the windows scheduler to run it consistently that's the problem. 但是我在使它能够一致且可靠地运行方面遇到问题,脚本本身运行良好,正在让Windows Scheduler一致地运行它,这就是问题所在。

The script is invoked every morning by windows scheduler at 1am as powershell.exe with the command arguments: 该脚本由Windows调度程序每天早上1点以powershell.exe的命令参数调用:

-windowstyle Normal  -NoExit -file "d:\work\PwrShellScripts\FlmToDb_010.ps1"

Once invoked, the script will run continuously until 11pm at night when it will exit. 一旦调用,脚本将连续运行直到晚上11点退出。

The script itself works reliably, but the scheduling only works nine times out of ten, once in a while it fails with the error: 脚本本身可以可靠地运行,但是调度仅在十分之九的时间内有效,有时会因以下错误而失败:

Task Scheduler did not launch task "\\DailyFlm" because instance "{aa18e048-d8b2-4e16-8737-fc7babbb609e}" of the same task is already running. 任务计划程序未启动任务“ \\ DailyFlm”,因为同一任务的实例“ {aa18e048-d8b2-4e16-8737-fc7babbb609e}”已在运行。

The question is, how to get the script to run reliably every day? 问题是,如何使脚本每天可靠运行?

Other info that may be relevant….... 其他可能相关的信息.......

The arguments -windowstyle Normal -NoExit mean that the powershell script runs in a command window (rather than as background process) and the window will remain open if the session ends. 参数-windowstyle Normal -NoExit表示-windowstyle Normal -NoExit脚本在命令窗口中运行(而不是作为后台进程),并且如果会话结束,则该窗口将保持打开状态。

This is done for two reasons, firstly it provides a visual indication that the process is actually running, and secondly if the process fails, it allows the error message to be inspected. 这样做有两个原因,首先,它可以直观地表明该进程实际上正在运行,其次,如果该进程失败,则可以检查错误消息。 The powershell script doesn't include any file logging, so running it in a command prompt also allows me to confirm that the previous days session made a clean exit when it stopped. powershell脚本不包含任何文件记录,因此在命令提示符下运行它也使我能够确认前几天的会话在停止时已干净退出。

One of the issues is that because the process works 90% of the time, if I make any tweaks I have to wait 10 days or more to confirm if they've really worked! 问题之一是因为该过程在90%的时间内都有效,所以如果我进行任何调整,我必须等待10天或更长时间才能确认它们是否确实有效!

I suspect that the issue may be related to the fact that the console remains open ( -NoExit ) when the script exits. 我怀疑问题可能与退出脚本时控制台保持打开状态( -NoExit )有关。 Most of the time windows seems to recognise that although the console is still open, the associated script has exited. 在大多数情况下,窗口似乎都可以识别出尽管控制台仍处于打开状态,但相关的脚本已退出。

My guess is that occasionally it decides that since the console is open the process is still running. 我的猜测是,它偶尔决定自控制台打开以来该进程仍在运行。 I'm unable to spot any difference between those occasions when the scheduling works fine and those when it doesn't. 在无法正常进行计划的情况与无法正常进行计划的情况之间,我无法发现任何差异。

Any suggestions? 有什么建议么?

Updates... The scheduler fails to start the job on average once every 10 days. 更新...调度程序平均每10天无法启动一次作业。 I would prefer to keep the script running in the foreground, it makes monitoring it's progress so much easier, and makes it so obvious if it does fail. 我宁愿让脚本在前台运行,它使监视进度变得非常容易,并且如果确实失败了,则变得非常明显。

the same task is already running. 相同的任务已经在运行。

The script may do what it's supposed while it runs, but there is one flaw... it's not closing properly. 该脚本可以在运行时按预期的方式运行,但是存在一个缺陷……它无法正确关闭。 It's exactly the -NoExit issue you talk about. 正是您所谈论的-NoExit问题。 When you run a powershell task, the process name is powershell.exe , with it's associated process id, and that's how the task scheduler knows if it's finished or not. 当您运行Powershell任务时,进程名称为powershell.exe ,具有关联的进程ID,这就是任务计划程序知道其是否完成的方式。

To fix this, I suggest writing a script to kill all powershell.exe processes and scheduling it to run right at 12:55am every day. 为了解决这个问题,我建议编写一个脚本来杀死所有powershell.exe进程,并将其安排为每天凌晨12:55运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM