LUA NGINX Examples
Jump to navigation
Jump to search
location = /healthcheck {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.status = 500
return
end
local ps = io.popen("/bin/ps aux >/tmp/ps.out", "w")
ps:close()
local ps = io.popen("grep -c '[j]ava.*63' /tmp/ps.out")
local output = tostring(ps:read())
ps:close()
if string.match(output, '4') then
ngx.status = 200
else
ngx.status = 500
end
}
}
<br /> f = assert (io.popen ("ls -l"))
for line in f:lines() do
print(line)
end -- for loop
f:close()
An alternative to using pipes, if you want to capture operating system output, is to redirect command output to a temporary file, like this:
-- get a temporary file name
n = os.tmpname ()
-- execute a command
os.execute ("dir > " .. n)
-- display output
for line in io.lines (n) do
print (line)
end
-- remove temporary file
os.remove (n)