如何等待直到cmd命令结束才执行下一个任务? [英] How to wait until a cmd command ends until performing the next task?

查看:1712
本文介绍了如何等待直到cmd命令结束才执行下一个任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a script that uses Ctrl+j to run

  1. mongod(mongo服务器)
  2. mongo(mongo数据库)
  3. npm start(启动节点的Web服务器)
  4. 在Chrome中打开localhost:3000.
  1. mongod (the mongo server)
  2. mongo (the mongo database)
  3. npm start (starts node's web server)
  4. Opens localhost:3000 in Chrome.

每个任务必须准备好才能开始下一个任务.例如,mongod是mongo服务器,因此如果mongo在服务器就绪之前启动,则会发生错误.

Each task must be ready before the next one can start. For instance, mongod is the mongo server, so if mongo starts before the server is ready, an error will occur.

这是我的剧本:

// 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

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


更改了代码的结构,以使重要部分更靠近顶部.

Is there a way to wait until the services are completely started before moving to the next task?


Changed the structure of the code to put the important section closer to the top.

推荐答案

根据我的评论,尝试:

    ...
    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
    ...

高度

这篇关于如何等待直到cmd命令结束才执行下一个任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆