Search code examples
cmdlua

How to keep os.execute(...) window open in Lua?


I have a simple Lua project that runs a NodeJS script every few minutes. The NodeJS script sets a function Interval and makes a request to an API every second or so.

My Lua looks like this:

function Initialize()
    Running = false
end

function Update()
    if not Running then
        Running = true
        os.execute(table.concat({"npm", "i"}, " "))
        os.execute(table.concat({"npm", "start"}, " "))
    end
end

How do I keep the Command Prompt window that opens with os.execute() open? It closes after the command is finished but I want to see the output before it closes. No, I don't want the result in my Lua code, I'd just like to see the window.

Thanks in advance!

EDIT: I'm Running on Windows 10


Solution

  • os.execute([[start "NPM" %ComSpec% /D /E:ON /K "call npm.cmd i & call npm.cmd start"]])
    

    (based on Mofi's code)

    Runs npm i and npm start sequentially on the same window

    os.execute([[start "npm i" %ComSpec% /D /E:ON /K "call npm.cmd i"]])
    os.execute([[start "npm start" %ComSpec% /D /E:ON /K "call npm.cmd start"]])
    

    Runs npm i and npm start sequentially on separate windows.

    Explanation: [[ is to escape quotes and lua sequences. start runs command window with some options and executes "npm i".

    I am not sure if call and .cmd are necessary (it runs without), but I've kept on Mofi recommendation.