1
votes

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

1
The usage of os.execute in LUA is like using function system in C or C++. It means executed is on Windows the Windows command processor %SystemRoot%\System32\cmd.exe with option /c to close the command process after finishing the execution of command line and the command line defined in the LUA script appended as additional arguments. So the first os.execute results in execution of C:\Windows\System32\cmd.exe /c npm i and the second in C:\Windows\System32\cmd.exe /c npm start. - Mofi
Open a command prompt, run cmd /? and read the output help. There is explained how the Windows command processor cmd.exe interprets the argument string(s) after option /C or option /K which would result in keeping command processor running after finishing the execution of the specified command line. In the command prompt window run where npm and output is the full qualified file name. You can see that npm is on Windows npm.cmd, a batch file which is very important to know. - Mofi
What you want to execute with os.execute is something like start "NPM" %ComSpec% /D /E:ON /K "call npm.cmd i & call npm.cmd start". That looks crazy and it is. There is started the Windows command processor cmd.exe with option /c by os.execute to execute a command line with its internal command start to start one more command process with a new console window with title NPM with ignoring the AutoRun registry value and with command extensions enabled to call two times npm.cmd with keeping this second command process running. - Mofi

1 Answers

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