lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Oct 28, 2013 at 12:52 PM, Ingo Struck <lua@ingostruck.de> wrote:
Hello Rena,

On Mon, Oct 28, 2013 at 12:29:37PM -0400, Rena wrote:
> > On Mon, Oct 28, 2013 at 3:35 PM, Rena <hyperhacker@gmail.com> wrote:
> > > In my app I open a few processes using io.popen(), then read their output
> > > using posix.fileno() and posix.rpoll() to see if there's data available.
> > > When my app shuts down however (using os.exit(0)), even if I close the
> > > process file handles, they remain running in the background. How do I
> > > prevent this? I do both process:close() and posix.close(fd). (I found
> > > without the latter, the former would just hang...)
> >
> > You can either send a kill (posix.kill) signal to the child, or make
> > the child test to see if the pipe is still open - it depends if it is
> > doing anything or is blocked, and how much it is under your control.
> But how do I get the child's PID?

You can't using io.popen(). We had exactly the same problem
with io.popen and thus re-implemented it  (but that impl had
some other ugly quirks).

Try not to mix luaposix and standard system stuff.
You may want to use luaposix fork + pipe, cf.
http://luaposix.github.io/luaposix/docs/examples/fork.lua.html

HTH and kind regards

Ingo


I see, but what if the process I want to spawn isn't another Lua process? I've tried this:

#!/usr/bin/env lua5.1
local posix = require('posix')
local function spawn(program, ...)
    local read, write = posix.pipe()
    local cpid = posix.fork()
    if cpid == 0 then --child
        assert(posix.execp(program, ...))
    else return {read=read, write=write, pid=cpid}
    end
end

local proc = assert(spawn('ping', '127.0.0.1'))
while true do
    local data = "" 1024))
    print("ping says:", data)
end

but ping doesn't write to the pipe, and just outputs to my terminal instead.

--
Sent from my Game Boy.