【问题标题】:autohotkey: how to set up a watchdogautohotkey:如何设置看门狗
【发布时间】:2014-07-13 06:30:36
【问题描述】:

我的 AHK 脚本的预期运行时间在 5 分钟到 25 分钟之间,具体取决于事件。它主要是从浏览器上抓取屏幕,搜索模式并相应地采取行动(通过单击屏幕上的某些区域)。并在一切完成后终止浏览器会话。我安排它从 Windows 任务调度程序每小时运行一次。

我的问题是,当网络出现问题或者我的工作站CPU有一个高级的时候,网页加载的时间就会变得不够(通常在15秒左右,加长是不可行的)因为内容可能会在 30 秒内发生变化)或者在过程中可能会挂起一些东西。

如果发生这种情况,我希望 AHK 脚本退出,并且在退出之前,我希望它终止所有浏览器会话(在这种特殊情况下为 chrome)

到目前为止,我无法提出“单一脚本”解决方案。

我目前的状态是启动一个脚本,它每 30 分钟监控一次文件的内容,如果内容与上次相比没有变化,则打开一个新的 google chrome 会话并发送 Shift kbd>-Ctrl-Q 键序列关闭所有chrome实例。同时,我的主脚本在完成后更新同一个文件,其中的数字与之前的值不同,并且它是唯一的。这种方法的不足:不杀掉这个看门狗脚本,不知道怎么杀掉另一个AHK脚本(挂了)。

我的最终愿望是将此功能构建到主脚本中,这很容易被挂起。

我的主脚本

send #r
send chrome.exe http://myURL{enter}
mousemove 200,200
send ^a
send ^c

;
; using a series of IfInString commands
; determine the condition
; then use mousemove, x, y and mouseclick, l
; commands, do my deed 

filedelete, c:\mydir\myfile
fileappend, newnumber, c:\mydir\myfile
exitapp

我的看门狗脚本

loop   ; forever
{
; old content is stored in variable PREV
fileread, newval, c:\mydir\myfile
if (newval = PREV)
{
  send #r
  send chrome.exe
  sleep 10000 ; wait for chrome window to pop up 
  send ^+Q
  ; I need a way to stop my hung AHK script here but don't know how
}
else
{
  PREV = %newval%
  sleep 1800000 ; sleep 30 minutes
}
} ; end of loop
f10::exitapp ; when I need to stop watchdog

当然,这些不是实际的脚本。只是给你一个想法。否则,有很多等待页面加载等。但它的要点就在那里。

提前感谢您的帮助

【问题讨论】:

  • 改用SetTimer
  • 我不知道这些脚本的目的是什么,但在我看来,如果你以编程方式提出这些请求,你可以让你的生活变得更轻松,例如使用WinHttpRequest
  • 就像@JoeDF 说的,将你的看门狗脚本中的循环放到SetTimer 中。

标签: autohotkey


【解决方案1】:
  • 我提供了一些 AutoHotkey 代码块,应该可以解决您描述的问题。
  • 我会尝试编写两个脚本,这样如果两个脚本都被终止, 有可能做一些检查并恢复,好像什么都没有 已经发生了,即增加更多的灵活性。
  • 我个人几乎从不使用SetTimer,我只使用带有Sleep 的循环, 在这种情况下,我认为这不会有助于改进脚本。
  • 我发现对于已挂起但没有错误消息表明它们已终止的脚本, 您可以检索 hWnd(窗口句柄)和 PID(进程 ID),从而终止它们。
  • 使用UrlDownloadToFile 可以更轻松地完成您描述的某些任务 或WinHttpRequest(参见论坛上的示例)下载网页,然后通过解析html。
  • 使用带有 COM(组件对象模型)的 Internet Explorer 可以更轻松地完成其他任务, 查询/操作实时网页中的网络元素。

-

;to launch Chrome and wait for it to open (with an optional delay afterwards)
;if the window is not seen within 60 seconds, ErrorLevel is set to 1 and action can be taken
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://www.google.com/"
WinWaitActive, ahk_class Chrome_WidgetWin_1, , 60
if ErrorLevel
{
MsgBox error ;put code here
}
else
{
Sleep 10000
;put code here
}

;to launch Internet Explorer and wait for it to open (with an optional delay afterwards)
Run, "C:\Program Files\Internet Explorer\iexplore.exe" "https://www.google.com/"
WinWaitActive, ahk_class IEFrame
Sleep 10000

;to terminate a script
DetectHiddenWindows, On
vPathScript = %A_ScriptDir%\my script.ahk
WinGet, hWnd, ID, %vPathScript% - AutoHotkey v ahk_class AutoHotkey
IfWinExist, ahk_id %hWnd%
{
WinGet, vPID, PID, ahk_id %hWnd%
Process, Close, %vPID%
}

【讨论】:

    【解决方案2】:

    进程看门狗使用: - 2 台带有共享文件夹的本地 PC - 3 个 ahk 脚本

    PC-1 ahk 进程脚本(永远循环) Windows 启动时自动启动

    LOOP   
       put a WD file on PC-2
       run some process
       wait some time 
       go to LOOP
    

    PC-1 ahk WD-1 脚本 Windows 启动时自动启动(永远循环)

        if initial startup set Counter and put WD file in local shared folder
    LOOP 
        wait some time
        if local WD file doesn't exist decrement Counter
        if Counter = Zero Send Email
        if local WD file exists reset Counter and delete local WD file
        go to LOOP
    

    PC-2 ahk WD-2 脚本 Windows 启动时自动启动(永远循环)

        if initial startup set Counter and put WD file in local shared folder
    LOOP 
        wait some time
        if local WD file doesn't exist decrement Counter
        if Counter = Zero Send Email
        if local WD file exists reset Counter and delete local WD File
        put WD file on PC-1 
        go to loop
    

    基本故障场景:

    Process script Fails ... PC-1 and PC-2 WD scripts will send emails     
    PC-1 Fails ... PC-2 WD script will send email
    PC-2 Fails ... PC-1 WD script will send email
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多