浏览 AI 使用教程
操作教程

Codex定期检查任务:Windows持久调度、状态变化与静默提醒

把Codex帮助编写的检查脚本交给Windows任务计划程序,演示每日检查本地状态JSON、保存上次状态、变化弹窗、失败记录与停用。

Codex项目开发与自动化工作流主视觉

先看重点:快速结论

  1. 完整 PowerShell 检查脚本,只在状态变化时提醒。
  2. Windows 当前用户交互任务适合个人电脑练习,不是全天候生产监控。
本文目录 · 跳到当前步骤

让 Codex 说“以后每天检查”,不等于电脑里已经有任务。本页把一项简单检查交给 Windows 任务计划程序:每日 09:00 读取本地 status.json,只有状态变化才弹窗。它演示调度和通知闭环,不抓取网页,也不验证真实服务健康。

输入和通知规则先固定

准备一个新的练习目录,将以下文件保存为 status.json:

json
{"status":"healthy","note":"teaching input"}

允许 healthy、degraded、unavailable 三种状态;note 不参与变化判断。上次没有记录时以 healthy 为初始参考,所以第一次读到 degraded 也会提醒。检查失败单独记录为 check-failed,不覆盖一份“服务正常”的结论。

运行者是当前已登录的 Windows 用户,通知是本机弹窗。电脑关机、用户注销或设备休眠会影响运行和及时性;需要全天候服务告警时,应把检查放到常开的服务与可靠通知通道,不能用本例承诺。

完整检查脚本

将下面保存为同目录 watch-status.ps1。脚本只用 ASCII 字符,避免 Windows PowerShell 5.1 对无 BOM 中文源文件的编码差异。它只读取练习状态文件,并在同目录写入日志与上次已报告状态。

powershell
$ErrorActionPreference = "Stop"
$inputFile = Join-Path $PSScriptRoot "status.json"
$stateFile = Join-Path $PSScriptRoot "last-reported.json"
$logFile = Join-Path $PSScriptRoot "runs.jsonl"
if (Test-Path -LiteralPath (Join-Path $PSScriptRoot "paused.flag")) { exit 0 }
try {
  $current = Get-Content -LiteralPath $inputFile -Raw | ConvertFrom-Json
  if ($current.status -notin @("healthy", "degraded", "unavailable")) {
    throw "Invalid status"
  }
  $observed = [string]$current.status
  $detail = "Read status.json"
} catch {
  $observed = "check-failed"
  $detail = "Cannot read valid status.json"
}
$previous = "healthy"
if (Test-Path -LiteralPath $stateFile) {
  $saved = Get-Content -LiteralPath $stateFile -Raw | ConvertFrom-Json
  if ($saved.status -notin @("healthy", "degraded", "unavailable", "check-failed")) {
    throw "Invalid last-reported.json; preserve it and inspect manually"
  }
  $previous = [string]$saved.status
}
$time = [DateTimeOffset]::Now.ToString("o")
[pscustomobject]@{time=$time; previous=$previous; observed=$observed; detail=$detail} |
  ConvertTo-Json -Compress | Add-Content -LiteralPath $logFile -Encoding utf8
if ($observed -ne $previous) {
  $shell = New-Object -ComObject WScript.Shell
  $null = $shell.Popup(
    ("State changed: " + $previous + " -> " + $observed + ". Check runs.jsonl."),
    20, "Local status check", 48)
}
[pscustomobject]@{status=$observed; reportedAt=$time} |
  ConvertTo-Json | Set-Content -LiteralPath $stateFile -Encoding utf8

last-reported.json 是去重状态,不是把异常批准为正常的基线。弹窗出现不证明用户看到了,20 秒会自动关闭;runs.jsonl 仍保留变化。示例状态文件若正在被别的程序写入,应让生产者用完整文件替换方式发布,避免读到半截 JSON。

先手动运行脚本,再将 status 从 healthy 改为 degraded,再运行。预期第二次弹窗,第三次状态未变时静默;改回 healthy 时报告恢复;把 JSON 故意写坏时报告 check-failed。同一失败持续时只写日志,不每天重复弹窗。完成练习后恢复合法输入。

注册每日持久任务

以下命令会真的在当前电脑注册一个任务,应在已经同意创建该练习任务后执行;阅读文章本身不会创建任务。先进入包含脚本的练习目录,再运行:

powershell
$scriptPath = (Resolve-Path -LiteralPath "./watch-status.ps1").Path
$account = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument ('-NoProfile -WindowStyle Hidden -File "' + $scriptPath + '"')
$trigger = New-ScheduledTaskTrigger -Daily -At "09:00"
$principal = New-ScheduledTaskPrincipal -UserId $account -LogonType Interactive -RunLevel Limited
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Minutes 2)
Register-ScheduledTask -TaskName "CodexLessonStatusWatch" -Action $action -Trigger $trigger -Principal $principal -Settings $settings

09:00 使用本机时间与时区。脚本执行策略可能阻止执行,应根据设备管理要求处理签名或策略,不在教程里强制绕过。任务同名时先查现有定义,不用 -Force 覆盖不明任务。

证明任务存在且能运行

powershell
Get-ScheduledTask -TaskName "CodexLessonStatusWatch" | Select-Object TaskName,State
Get-ScheduledTask -TaskName "CodexLessonStatusWatch" | Select-Object -ExpandProperty Triggers
Start-ScheduledTask -TaskName "CodexLessonStatusWatch"
Get-ScheduledTaskInfo -TaskName "CodexLessonStatusWatch"
Get-Content -LiteralPath "./runs.jsonl" -Tail 5

Start-ScheduledTask 发起后,等实际运行完成再查看 LastRunTime、LastTaskResult 和日志;启动命令返回不代表脚本已结束。模拟状态变化并验证弹窗后,还要等一次自然触发,才能确认时间配置生效。

关闭 Codex 聊天不会删除这个系统任务,但当前用户注销后本例交互任务无法正常显示通知。历史成功退出码不能证明现在的 status.json、脚本路径或通知仍正常。

暂停与恢复

powershell
Disable-ScheduledTask -TaskName "CodexLessonStatusWatch"
Enable-ScheduledTask -TaskName "CodexLessonStatusWatch"

临时需要阻止手动运行时,可在脚本目录创建 paused.flag;移除这个标记后恢复。停用调度不会撤回已经显示的通知,也不能修复输入错误。

本页提供可执行教学配置,没有替任何读者创建任务。正式交付记录应包括任务名、执行账号、触发时间、脚本路径、最后成功记录和一次变化通知证据;缺少这些,就只能说脚本已写好。

资料来源

下一步

继续阅读与你当前问题相关的教程: