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

 
 
 
 

openresty 使用lua-resty-shell 执行shell 脚本的更多相关文章

  1. Shell执行将脚本里的变量打印到指定日志文件

    首先需要定位获取任务的运行日志或者报错信息,才能定位问题. 通过shell调用有些脚本的话,日志信息会打印在shell里.不过也有用户在shell里调用正常,但是到crontab调用的时候就出错并且没 ...

  2. linux shell执行SQL脚本

    #!/bin/sh user="user" pass="pass" sqlplus -S $user/$pass select 1 from dual; exi ...

  3. Linux远程执行Shell命令或脚本

    ## 远程执行shell命令 ssh [user]@[server] '[command]' # eg. ssh root@192.168.1.1 'uptime' ## 远程执行本地shell脚本 ...

  4. 【转】linux 定时执行shell脚本

    在oracle 中可以利用dbms_job包定时执行pl/sql.sql过程,在像备份等需要在操作系统级定时任务只能采用crontab来完成 本文讲述crontab具体用法,以供备忘. 在oracle ...

  5. 【Telnet】使用Telnet协议连接到远程Shell执行脚本

    介绍 本文介绍如何通过Telnet协议连接到远程Shell,执行脚本,并获取执行结果: 相关文章: <[Jsch]使用SSH协议连接到远程Shell执行脚本>http://www.cnbl ...

  6. 【Jsch】使用SSH协议连接到远程Shell执行脚本

    如果大家熟悉Linux的话,一定对ssh,sftp,scp等命令非常熟悉,ssh是一个安全协议,用来在不同系统或者服务器之间进行安全连接,SSH 在连接和传送的过程中会加密所有的数据. 但是SSH一般 ...

  7. Linux中执行shell脚本的4种方法总结

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...

  8. Linux中执行shell脚本的4种方法

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...

  9. 执行shell脚本的几种方法及区别

    执行shell脚本的几种方法及区别 http://blog.csdn.net/lanxinju/article/details/6032368 (认真看) 注意:如果涉及到脚本之间的调用一定要用 . ...

随机推荐

  1. Instruments leak黑魔法定位内存泄漏

    leak是一款很赞的内存检查的工具,但在使用的过程中有点繁琐,至少有些底层的泄漏笔者还是不知道如何下手 下面介绍一下简单leak的使用: 首先你要确认你的target不会被拒绝,确保profile是d ...

  2. 关于collectionview布局的坑

    不知道写了多少次collectionview,步了很多坑,现在看来虽然达到了自己想要的结果,却不知道其中所以然.还是总结一下,免得再走弯路: 场景是这样的,我要定制一个显示选择图片的排列,想要实现横向 ...

  3. 网页的MVC模式简介

    #! /usr/bin/env python3 # -*- coding:utf-8 -*- #MVC:Model-View-Controller 模型-视图-控制器 #Python处理URL的函数就 ...

  4. mybatis-generator没有自动生成代码和Junit测试controller

    本来mybatis的generator想要自动生成增删改的,但是到后来语句就两个select,原因是数据中没有给字段加primary,就不会有删改增. 以及Controller的Junit测试 先导入 ...

  5. L300 3月英语课下

    重音 句中的实词(内容词)要被重读,读得重.长一点.句中的虚词(功能词)要被轻读或弱读,读得轻.短一点口语交流中,当虚词的在句中起到重要的表意作用时,会被重读. 连读 连读:把一个单词的尾音同下一个单 ...

  6. TensorFlow函数:tf.truncated_normal

    tf.truncated_normal函数 tf.truncated_normal( shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, ...

  7. MongoDB的安装及安装为windows服务

    首先在网上找了一篇教程,于是按着做,下载了最新版的安装包,解压以后目录是这样的: 没有bin目录啊 原因:zip没有经过编译 于是下载安装包,安装包是msi 一直按照next提示就可以了,但是记得选择 ...

  8. 深入了解HyperServer

    本文,我们将尝试深入了解uniGUI HyperServer. 可以将HyperServer所有功能分成三类: HyperServer和稳定性 HyperServer和可扩展性 HyperServer ...

  9. array_reverse()函数

    $a=array(1,2,4,5,6); print_r(array_reverse($a)); 结果:Array ( [0] => 6                              ...

  10. Python MRO_C3

    class A: pass class B(A): pass class C(A): pass class D(B, C): pass class E(C, A): pass class F(D, E ...