【问题标题】:How to wait until a cmd command ends until performing the next task?如何等到 cmd 命令结束直到执行下一个任务?
【发布时间】:2017-05-27 19:48:50
【问题描述】:

我有一个使用 Ctrl+j 运行的脚本

  1. mongod(mongo 服务器)
  2. mongo(mongo 数据库)
  3. npm start(启动节点的网络服务器)
  4. 在 Chrome 中打开 localhost:3000

每个任务都必须在下一个任务开始之前准备好。比如mongod是mongo服务器,所以如果mongo在服务器准备好之前启动,就会报错。

这是我的脚本:

// Start or stop mongod, mongo, node
^j::
    IfWinNotExist, npm // start everything up. This must be done in order, but the timing varies.
    {
        // Start the mongo server
        Run, mongod.exe
        WinGet, active_id, ID, A            // Get the id of the window
        GroupAdd, One, ahk_id %active_id%   // Add the window to a new Group so we can minimize them all later
        Sleep 1000   // This works, but the time varies. I'd like to replace it


        // Start the mongo database
        Run, mongo.exe
        WinGet, active_id, ID, A
        GroupAdd, One, ahk_id %active_id%
        Sleep 1000 // I'd like to replace this

        // Start the node server
        Run, cmd.exe
        Sleep 100
        send, cd webroot{\}{enter}
        Sleep 300
        send, npm start{enter}
        WinGet, active_id, ID, A 
        GroupAdd, One, ahk_id %active_id%
        Sleep 1000 // I'd like to replace this

        // Minimize all the cmd windows
        WinMinimize, ahk_group One

        // Always opens a new tab - but that's for another question...
        Run, chrome.exe http://localhost:3000

    } else {        // shut everything down if they're already running
        SetKeyDelay, 400
        ControlSend, ,^cy{enter}, npm
        SetKeyDelay, -1
        Sleep 1000
        WinClose, ahk_class ConsoleWindowClass

        SetTitleMatchMode 2
        ControlSend, ,^c, mongo
        Sleep 1000
        WinClose, ahk_class ConsoleWindowClass

        ControlSend, ,^c, mongod
        SetKeyDelay, 200,
        Sleep 1000
        WinClose, ahk_class ConsoleWindowClass
    }
Return

// Bonus for anyone that's interested in using this script.
// Recycle the node server in the background
!`::
    SetKeyDelay, 200
    ControlSend, ,^c y{enter} npm start{enter}, npm
Return

有没有办法等到服务完全启动后再进行下一个任务?


编辑:更改了代码结构,使重要部分更靠近顶部。

【问题讨论】:

标签: mean-stack autohotkey


【解决方案1】:

根据我的评论,尝试:

    ...
    SetKeyDelay, 400
    ControlSend, ,^cy{enter}, npm
    SetKeyDelay, -1
    Sleep 1000
    WinClose, ahk_class ConsoleWindowClass

    WinWaitClose, ahk_class ConsoleWindowClass, , 3

    SetTitleMatchMode 2
    ControlSend, ,^c, mongo
    Sleep 1000
    WinClose, ahk_class ConsoleWindowClass

    WinWaitClose, ahk_class ConsoleWindowClass, , 3
    ...

【讨论】:

  • 这很有见地,但我正试图摆脱 Sleep 命令。在您的示例中,Sleep 仍然是必需的,因为我们需要在关闭窗口之前等待命令结束。但是,在研究您的建议时,我偶然发现了Process。其中有一个wait 选项,但我不确定我是否可以按照我想要的方式使用它。调查它..
  • 另一种方法可能是使用一个或多个批处理文件来进行处理(或程序启动)。有一个有用的 cl 命令:START 命令运行程序:START "title" [/D path] [options] "command" [parameters] 每个 START 调用都会运行其参数中给出的命令并立即返回,除非使用 /WAIT 开关执行。您还可以使用批处理文件Exit 命令进行自我关闭(这样您就不需要 AHK 来关闭窗口)。 Hth
  • 哦,所以我突然想到你可以一次完成所有操作:cmd.exe /C start /wait mongo 我认为可以。
  • 太棒了!我会试试的。
【解决方案2】:

在 AutoHotkey 脚本中,一些替换 Sleep 的方法是:
- WinWaitActive 或 WinWait('win wait 存在')。
- 在创建时通过 Run 命令检索进程的 PID。
例如

DetectHiddenWindows, On

Run, mongod.exe, , , vPID1
WinWaitActive, ahk_pid %vPID1%
WinGet, hWnd1, ID, ahk_pid %vPID1%

Run, mongod.exe, , , vPID2
WinWaitActive, ahk_pid %vPID2%
WinGet, hWnd2, ID, ahk_pid %vPID2%

;then later

WinMinimize, ahk_id %hWnd1%
WinMinimize, ahk_id %hWnd2%

AutoHotkey 擅长做 bat 文件所做的一切, 例如,如果可以检索 StdOut。 一般来说,您不需要打开 cmd.exe 并输入内容。

您可以使用 cmd.exe (ComSpec) 并指定路径/​​目标。
此示例选择文件夹中的文件。
(在 Windows 7 上测试。)

vPathNotepad = C:\Windows\System32\notepad.exe
Run, %ComSpec% /c explorer.exe /select`, "%vPathNotepad%",, Hide

注意:文字逗号需要在运行命令中用反引号转义, 但不是在分配变量时,请注意上面“选择”一词后面的反引号,但下面没有。

或者更容易遵循的重写:

vPathNotepad = C:\Windows\System32\notepad.exe
vTarget = %ComSpec% /c explorer.exe /select, "%vPathNotepad%"
Run, %vTarget%,, Hide

如果您希望进程运行并等待它终止, 在执行任何其他操作之前,您可以使用 RunWait 而不是 Run。
例如

vPath = C:\webroot\npm.exe ;or whatever the path is
RunWait, %vPath%

也可以在窗口隐藏/最小化的情况下启动进程。 例如

Run, %vPath%, , Hide
Run, %vPath%, , Min
RunWait, %vPath%, , Hide
RunWait, %vPath%, , Min

mongo 和 mongod 是命令行还是 GUI? 如果它们是 GUI,则可能还有其他可以检测到的东西, 确定要做什么,例如从控件中检索文本。

[编辑:]
有用的链接:

在每个文件和(子)文件夹甚至包含空格的文件和(子)文件夹上运行命令行工具 - AutoHotkey 社区
https://autohotkey.com/boards/viewtopic.php?f=5&t=26819

关于 Chrome,'总是打开一个新标签 - 但这是另一个问题......',我写的一些功能:
Firefox/Chrome,获取标签名称/焦点标签 - AutoHotkey 社区
https://autohotkey.com/boards/viewtopic.php?f=6&t=26947&p=126248#p126248

【讨论】:

  • 我测试了你的代码,但是WinWaitActiveRunWaitwindow 完成打开时触发,而不是在进程完成时触发 - 如果使用 @987654332 @。如果运行mongod.exeRunWait 将等到进程完成关闭,这意味着直到我发送 kill 命令。但是,我正在阅读Drugoy AHK scripts。您的 Chrome 选项卡查找器 (ACC) 需要其中之一。好像里面有一些很棒的东西。谢谢!
  • 可能,检索 StdOut 可用于检索程序的状态。谢谢回复。 Firefox/Chrome 脚本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 2016-06-20
相关资源
最近更新 更多