1

I'm attempting to constantly read and parse a log file (Minecraft log file) by using io.popen in tandem with Ubuntu's tail command so that I can send some messages upon certain events.

Now, I have mostly everything working here, except one small issue. After a while of reading, the entire program just freezes.

Here is the relevant code:

    -- Open the tail command, return a file handle for it.
    local pop = io.popen(config.listen_command)

    -- Simply read a single line, I've pulled this into its own
    -- function so that if this ever needs changing I can do so
    -- easily.
    local function get_line()
      logger:log(4, "READ LINE")
      return pop:read("*l")
    end

    -- For each line in the log file, check if it matches any
    -- of a list of patterns, return the matches and the
    -- pattern information if so.
    local function match_line()
      local line = get_line()
      logger:log(4, "Line: %s", line)

      -- This all works, and I've tested that it's not freezing
      -- here. I've just included it for completion of the call
      -- -stack.
      for event_type, data in pairs(config.message_patterns) do
        for event_name, pattern in pairs(data) do
          local matches = {line:match(pattern)}
          if matches[1] then
            return event_type, event_name, matches
          end
        end
      end
    end

    -- The main loop, simply read a line and send a message
    -- if there was a match.
    logger:log(4, "Main loop begin.")
    while true do
      local event_type, event_name, matches = match_line()
      -- ...
      -- The rest of the code here is not relevant.

config.listen_command = "tail -F --lines=1 latest.log"

The issue is in the get_line function. After a while of reading the log file, it completely freezes on the pop:read("*l"). It prints the READ LINE message, but never prints the Line: <whatever data here> line.

This is a really strange issue that I've been getting really confused about. I've tried swapping to different distributions of Lua (Luvit, LuaJIT, Lua) and a very large amount of debugging, changing small things, rerunning, ... But I cannot think of anything that'd be causing this.

Perhaps there's something small I've missed.

So my question here is this: Why is pop:read("*l") freezing, even though more data is being outputted to the logfile? Is there a way to fix this? Perhaps to detect if the next read will freeze indefinitely, so I can try closing the popen'ed file and re-open it (or to preferably stop it happening altogether?)

0 Answers0