openresty 使用lua-resty-shell 执行shell 脚本

lua-resty-shell 是一个很不错的项目,让我们可以无阻塞的执行shell命令,之间的通信
是通过socket (一般是unix socket)

环境准备

  • docker-compose 文件
version: "3"
services:
  app:
   build: ./
   ports:
   - "8080:80"
   volumes:
   - "./app/:/opt/app/"
   - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  • dockerfile
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
WORKDIR /sockproc
COPY ./sockproc/ /sockproc/
RUN make sockproc
COPY entrypoint.sh /entrypoint.sh
COPY sockproc.sh /sockproc.sh
COPY shell.lua /usr/local/openresty/lualib/resty/shell.lua
ENTRYPOINT [ "/entrypoint.sh" ]
  • dockerfile 说明
    dockerfile 同时进行了socket 服务的构建以及shell lua 封装的copy,entrypoint 进行服务的启动
entrypoint.sh:
#!/bin/sh
sh /sockproc.sh
exec /usr/local/openresty/bin/openresty -g "daemon off;"
sockproc.sh:
#!/bin/sh
/sockproc/sockproc /tmp/shell.sock

openresty 集成测试

  • lua 调用代码
app/app.lua

local shell = require("resty.shell")
local args = {
   socket = "unix:/tmp/shell.sock",
}
function call()
    local status, out, err = shell.execute("cat /proc/sys/kernel/random/uuid", args)
    ngx.say(out)
end
return call
  • openresty content_by_Lua 阶段调用lua shell 封装
+ user root;

http {
+ lua_package_path '/opt/app/?.lua;;';

+ location /test {
+ content_by_lua_block {
+ require("app")()
+ }
  • 测试效果

参考资料

https://github.com/juce/lua-resty-shell
https://github.com/rongfengliang/lua-resty-shell-docker-running

posted on   荣锋亮  阅读(4606)  评论(2编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2013-12-06 使用cglib动态创建javabean

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示