9951 explained code solutions for 126 technologies


nginx-luaRead shell command output with io.popen


server {
  location / {
    content_by_lua_block {
      local handle = io.popen('date')
      local out = handle:read("*a")
      handle:close()
      ngx.say(out)
    }
  }
}ctrl + c
content_by_lua_block

nginx-lua module directive to specify block of Lua code

io.popen

execute specified shell command and return handle to read output

handle:read

read data from handle (command output)

handle:close()

close handle

ngx.say

output given text to client