I have a script that uses Ctrl+j to run
mongod
(the mongo server)mongo
(the mongo database)npm start
(starts node's web server)- Opens
localhost:3000
in Chrome.
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.
Here's my script:
// 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?
Edit: Changed the structure of the code to put the important section closer to the top.