r_a*_*all 8 windows windows-task-scheduler
Windows 7 任务计划程序允许我在计算机空闲时运行任务,但是当计算机从空闲状态恢复或不再空闲时,似乎没有任何明显的方法来运行任务。
当计算机不再空闲时,肯定会在 Windows 中触发一些事件(事件日志?)?或者某种方法来捕获计算机不再空闲的事实,并通过计划任务响应它?
我该怎么做?
或者,在最坏的情况下,是否有命令行程序可以在计算机进入/退出空闲状态时调用命令或事件?
[更新:]我对 Diogo Rocha 的回复中的方法有效。我通过 py2exe 从这个脚本中创建了一个空的可执行文件:
import sys
import time
#restart a pause every twenty seconds, with two functions that call each other.
def call_pause():
pause()
def pause():
time.sleep(20)
call_pause()
call_pause()
Run Code Online (Sandbox Code Playgroud)
-- 并在 Windows 中设置一个计划任务,这是导出的 HTML:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2012-04-27T17:40:46.8871631</Date>
<Author>GENIUS-BREATH-COMPY</Author>
<Description>This task runs ProgA when the computer enters an idle state, and terminates ProgA when the computer *leaves* an idle state. The is all for scheduled TaskB, which periodically runs a batch that tests whether ProgA is running. If ProgA is not running (because this task terminated it), ProgB runs (as the computer is NOT idle). If ProgA *is* running, TaskB's batch does not run ProgB.</Description>
</RegistrationInfo>
<Triggers>
<IdleTrigger>
<Enabled>true</Enabled>
</IdleTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<Duration>PT1M</Duration>
<WaitTimeout>PT0S</WaitTimeout>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>true</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>true</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT1M</Interval>
<Count>3</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\path_to\nullExecutable</Command>
</Exec>
</Actions>
</Task>
Run Code Online (Sandbox Code Playgroud)
让我的电脑闲置了 15 分钟。任务管理器显示空可执行文件正在运行。我一移动鼠标,它就让计算机脱离了空闲状态,空可执行文件从任务列表中消失了。
从这里开始,这是一个设置任务(或程序——我正在用 Python 和 py2exe 做的)的问题,它使用 pslist(带有 -accepteula 开关,以便在部署到它的计算机上实际运行程序)到检查空 exe 是否正在运行。如果它正在运行,%ERRORLEVEL% 环境变量设置为 0,因为 pslist 运行没有错误。如果该环境变量为 1,则它运行时出错(它没有找到正在运行的可执行文件)。如果计算机不空闲,我正在批处理脚本中利用该环境变量来运行另一个任务。
我认为不可能实现任何方法来检测空闲事件(进入或退出空闲状态)上的触发“边界”,但是有一个命令可以强制您的 Windows 进入空闲状态并运行空闲触发任务:
Rundll32.exe advapi32.dll,ProcessIdleTasks
Run Code Online (Sandbox Code Playgroud)
您可以结合另一个事件(来自事件日志)来运行此命令,该命令又会触发另一个必须在空闲状态下运行的任务。据您所知,您可以自由组合您可能想要的任何任务。
| 归档时间: |
|
| 查看次数: |
2491 次 |
| 最近记录: |