Use lua os.execute in windows to launch a program with out a flash of CMD
Solution 1
Lua's os.execute command is based on the C standard library "shell" function. In Windows, this function will always create a command window, and it will always halt your current process until the window finishes. The latter also happens in Linux.
There is ultimately no way around this. Not through the Lua standard API. Because Lua needs to be light-weight and platform independent, the API is not allowed to use OS-dependent native APIs.
Your best bet would be to use the Lua Ex-Api module. It is effectively abandonware, and you may need to patch up a few compiler issues (I'm guessing the Windows port wasn't their first priority). But it is a reasonably good way to spawn processes. You can choose to wait until it finishes yourself, or let them run in parallel. And it won't throw up a command prompt window, unless the application itself uses one.
Solution 2
This is the piece of code I use to call a batch from Lua, maybe help. In win console (command prompt) open and execute, same in unix (mac|nix)
-- sBatchFile = .bat for windows, .sh for x
function vfFork2(sBatchFile)
local b = package.cpath:match("%p[\\|/]?%p(%a+)")
if b == "dll" then
-- windows
os.execute('start cmd /k call "'..sBatchFile..'"')
elseif b == "dylib" then
-- macos
os.execute('chmod +x "'..sBatchFile..'"')
os.execute('open -a Terminal.app "'..sBatchFile..'"')
elseif b == "so" then
-- Linux
os.execute('chmod +x "'..sBatchFile..'"')
os.execute('xterm -hold -e "'..sBatchFile..'" & ')
end
end
Related videos on Youtube

Jane T
Long time programmer, using a variety of Languages, currently learning Lua for a new Project. Programming languages can be learnt by heart but mastered by practice. -Majid-
Updated on June 04, 2022Comments
-
Jane T 12 months
I am happily launching a program in a windows system from Lua using
strProgram = '"C:\\Program Files\\Ps Pad\\PSPad.exe"' strCmd = 'start "" '..strProgram os.execute(strCmd)
This works correctly, launching the program and the script finishing. How ever it flashes up a command window for a fraction of a second, does any one have a way from Lua to launch a program.
-
BMitch almost 12 yearsFrom the docs, it looks like they are running it through the OS shell (sorry, I'm on Linux so I couldn't test this myself). Unless someone knows a Lua trick, you may need to write your own function in the host language (e.g. C) that does a
fork/exec
instead of thesystem
call and export that API out to Lua.
-
-
Jane T almost 12 yearsThanks for that, I am afraid that looks a bit beyond my skill set, I will pass it on the person whose software has Lua in and see if he can add a function for me and I'll point him at that code if he needs it.
-
Tamás Szelei about 8 yearsI believe there is at least one way to execute a command without the console popping up on windows with no extra library, using io.popen. Please see my answer.
-
handle almost 8 yearsNegative, Win7 64 bit and it behaves the same as with just popen or execute (Lua 5.2.3, linked)
-
Tamás Szelei almost 8 years@handle There is obviously something different about our setups then. I'm using this code almost every day in a build system and I have no command windows popping up at all.
-
handle almost 8 yearsnot doubting you, just preparing others that this may not work despite your success.
-
Tamás Szelei almost 8 years@handle sure, I added a warning to my post as well.