如果脚本从/etc/rc.local运行,则lua os.execute不起作用

时间:2016-01-07 14:25:25

标签: bash curl lua init

我有一个简单的lua脚本,用于侦听特定事件,然后向twilio.com Web服务发出POST请求以启动呼叫,然后发送邮件。

#!/usr/bin/lua
-- February 2015, Suxsem

local sock      = require("socket")
local mosq      = require("mosquitto")

local mqtt = mosq.new("sonny_lua_scattato", true)
mqtt:login_set("***", "***")

local call = function (from, to)
        os.execute([[curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/***/Calls.json' \
                --data-urlencode 'To=]] .. to .. [[' \
                --data-urlencode 'From=]] .. from .. [[' \
                -d 'Url=https://demo.twilio.com/welcome/voice/' \
                -d 'Method=GET' \
                -d 'FallbackMethod=GET' \
                -d 'StatusCallbackMethod=GET' \
                -d 'Timeout=20' \
                -d 'Record=false' \
                -u ***:***> twiliolog.txt]])
end

local callback = function (mid, topic, message)
        print("Received: " .. topic .. ": " .. message)

            if topic == "ArseniAlarm/Scattato" then

                local call_from = "+393317893***"
                local call_to = {"+393299573***",
                                      "+393473621***",
                                      "+393492829***"}

                for i, to in ipairs(call_to) do
                        call(call_from, to)
                end

                local rcpt = {"semer***.stef***@hotmail.it",
                                "miky.semer***@live.it"}

                local sbj = "ANTIFURTO ARSENI"
                local bdy = "ANTIFURTO ARSENI - " .. os.date("%T - %d/%m/%Y")
                os.execute('echo -e "To: ' .. table.concat(rcpt, ",") .. '\r\nSubject: ' .. sbj .. '\r\n\r\n' .. bdy .. '" | msmtp --from=default -t')

        end

end

mqtt:callback_set("ON_MESSAGE", callback)

local connected = false

mqtt:callback_set("ON_CONNECT", function ()
        print("Connected")
        connected = true
        mqtt:subscribe("ArseniAlarm/Scattato", 2)
end)
mqtt:callback_set("ON_DISCONNECT", function ()
        print("Disconnected")
        connected = false
end)

mqtt:connect("127.0.0.1", 1883, 60)

while true do
        mqtt:loop()
        if connected then
                sock.sleep(1.0) -- seconds
        else
                sock.sleep(5.0)
                mqtt:reconnect()
        end
end

现在......我使用以下命令运行脚本:

cd /root/mqtt_client/ ; lua /root/mqtt_client/mqtt_scattato.lua &

一切正常,我接到电话和邮件,并在twiliolog.txt(如果你看看我做的代码"卷曲...> twiliolog.txt"开始通话)我可以看到twilio的回应。

BUT

如果我在/etc/rc.local中输入相同的命令(cd / root / mqtt_client /; lua /root/mqtt_client/mqtt_scattato.lua&),会发生一些奇怪的事情: 1)我收到邮件但没有收到电话! 2)twiliolog.txt(curl的输出)已正确创建,但它是空的!

我的平台是一个带有openwrt屏障断路器的路由器。

我真的希望你能帮助我,谢谢!

1 个答案:

答案 0 :(得分:0)

我最终完全删除了os.execute调用。 我现在正在使用lua native https套接字:

local https = require("ssl.https")
local call = function (from, to)
        local body = "To=" .. to .. "&From=" .. from .. "&Url=https://demo.twilio.com/welcome/voice/&Timeout=20&Record=false"
        https.request {
                protocol = "tlsv1",
                method="POST",
                url="https://api.twilio.com/2010-04-01/Accounts/***/Calls.json",
                source=ltn12.source.string(body),
                headers = {["content-length"] = #body, ["content-type"] = "application/x-www-form-urlencoded"},
                user="***",
                password="***",
        }
end

我仍在使用os.execute发送邮件,它工作正常,所以我认为问题与启动时curl + os.execute +启动脚本的组合有关。

我仍然不知道什么问题可能导致这种行为