lua-users home
lua-l archive

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


On Sat, Apr 10, 2021 at 5:12 PM Nicholas Joll
<nicholasjoll@mailfence.com> wrote:

>  Jeff Pohlmeyer thought that he had a way that I could
> achieve my goal in pure lua. Yet, I can't get his code to work.

I should have mentioned I am using the GIT pre-release version of
luasposix, not sure about glob() arguments in earlier versions. The
code I posted is not very "smart" about what it tries to match, i.e.
the "real" pidof program will match "bash" or "/usr/bin/bash"; mine
only directly compares the first token in /proc/{PID}/cmdline or the
program pointed to by the /proc/{PID}/exe symbolic link.

You might try adding a print() statement to the glob loop to view what
(if anything) it is finding and visually compare it to what you're
looking for.

Maybe something like this:


function pidof_cmd(cmd)
  local pids={}
  for _, p in pairs(glob('/proc/[0-9]*/cmdline')) do
    local f=io.open(p)
    if f then
      local s=f:read('*a')
      if s and #s>0 then
        s=s:gsub('\0.*$','')

        print(s) -- <= *** DEBUG ***

        if s==cmd then
          table.insert(pids,(p:gsub('^/proc/(%d+)/.*$', '%1')))
        end
      end
    end
  end
  return pids
end

function pidof_exe(exe)
 local pids={}
  for _, p in pairs(glob('/proc/[0-9]*/exe')) do
    local s=posix.readlink(p)

    print(s) -- <= *** DEBUG ***

    if s==exe then
      table.insert(pids,(p:gsub('^/proc/(%d+)/.*$', '%1')))
    end
  end
  return pids
end

 -- Jeff