If you're using luajit you can use the foreign function interface to directly call the OS api functions. e.g. for Linux or similar.
local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
int fork(void);
int execlp(const char* file, const char *arg, ...);
int waitpid(int pid, int *status, int options);
void _exit(int status);
unsigned int sleep(unsigned int seconds);
]]
local pid = C.fork()
if pid > 0 then -- parent
print("Waiting for child process:");
local status = ffi.new('int[1]')
local WNOHANG = 1
local done = false
while not done do
local id = C.waitpid(-1, status, WNOHANG)
if id == pid then
done = true
elseif pid < 0 then
print("error occurred")
os.exit(-1)
else
print("status=",status[0])
C.sleep(1)
-- do other stuff. We aren't waiting
end
end
print("Child exited with status=", status[0])
elseif pid == 0 then -- child
print("Starting child")
C.execlp('sleep', 'sleep', '5')
C._exit(-1) -- exec never returns
elseif pid < 0 then -- failure
print("failed to fork")
end
You'll see that with the WNOHANG = 1 you can still get a result back to see if the child has exited but then carry on to do other things.