如何在Codex CLI完成任务时获取清晰的GUI通知?
Codex CLI任务完成时的GUI通知解决方案
核心思路
利用Codex CLI官方支持的钩子机制,在任务完成(成功/失败)时调用系统原生的GUI通知工具,替代或补充现有的终端提示音与tmux高亮,确保你在处理其他事务时能及时收到清晰提醒。
1. 基础配置(Linux系统,使用notify-send)
步骤1:找到Codex CLI配置文件
通常配置文件位于 ~/.codex/config.yaml,如果不存在可以手动创建。
步骤2:添加任务完成钩子
在配置文件中添加post-task相关钩子,指定触发时执行的通知命令:
hooks: # 所有任务完成后触发(无论成功/失败) post-task: command: "notify-send 'Codex任务完成' '耗时任务已执行完毕,可切换回终端查看结果'" # 可选:区分成功/失败的通知 post-task-success: command: "notify-send -u normal -i dialog-ok 'Codex任务成功' '任务执行完成,结果可用'" post-task-failure: command: "notify-send -u critical -i dialog-error 'Codex任务失败' '任务执行出错,请检查终端日志'"
步骤3:增强通知显眼度
给notify-send添加参数提升通知优先级与展示时长:
# 高优先级通知,显示15秒并附带图标 notify-send -u critical -t 15000 -i dialog-information 'Codex任务完成' '耗时任务已执行完毕'
参数说明:
-u critical:设置最高优先级,通知会置顶显示,避免被其他窗口遮挡-t 15000:通知持续显示15秒(默认仅几秒)-i:指定系统图标,让通知更直观
2. 跨平台适配方案
macOS系统
使用osascript调用系统原生通知,可同时搭配提示音:
hooks: post-task: command: "osascript -e 'display notification \"耗时任务已执行完毕,可查看结果\" with title \"Codex任务完成\" sound name \"Hero\"'"
sound name "Hero":指定系统自带的大音量提示音,解决终端提示音过小的问题
Windows系统
使用PowerShell调用UWP原生通知,需确保系统支持:
hooks: post-task: command: "powershell -Command \"[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null; $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02); $textNodes = $template.GetElementsByTagName('text'); $textNodes.Item(0).AppendChild($template.CreateTextNode('Codex任务完成')) > $null; $textNodes.Item(1).AppendChild($template.CreateTextNode('耗时任务已执行完毕,可查看结果')) > $null; $toast = [Windows.UI.Notifications.ToastNotification]::new($template); [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('Codex CLI').Show($toast)\""
3. 额外优化:结合提示音强化提醒
如果担心离开电脑时看不到通知,可在钩子命令中同时播放大音量提示音:
- Linux:搭配
paplay播放系统声音post-task: command: "notify-send -u critical 'Codex任务完成' '耗时任务已执行完毕' && paplay /usr/share/sounds/freedesktop/stereo/complete.oga" - macOS:已在
osascript命令中包含声音参数,无需额外配置 - Windows:在PowerShell命令后添加声音播放命令
powershell -Command "...(上述通知命令); [System.Media.SoundPlayer]::new('C:\Windows\Media\notify.wav').PlaySync()"
4. 验证配置
保存配置文件后,执行一个简单的Codex任务测试(如codex-cli generate "写一个Python版HelloWorld"),确认通知正常弹出即可。
内容的提问来源于stack exchange,提问作者Ciro Santilli OurBigBook.com




