Re: Error executing script command returned: 256
Posted: Friday 05 February 2021 9:54
In that case I don't understand why your script is executed in the background, because there's no ampersand in the script?
Join 16,000+ members discussing home automation with Domoticz. Get help with Zigbee, ZWave, MQTT, scripting, and 150+ integrations
https://forum.domoticz.com/
Because the script does not use the Lua functions os.execute or io.popen but a separate (and async) thread coded in the domoticz program called by the dzVents function dz.executeShellCommand()JanvdW wrote: Friday 05 February 2021 9:54 In that case I don't understand why your script is executed in the background, because there's no ampersand in the script?
Code: Select all
return { on = { timer = { "every 5 minutes" } },
logging = { level = domoticz.LOG_INFO, marker = "Ping smartphones" },
data = { timeoutcount = { initial = {} } },
execute = function(dz, device)
local devices_ping = { ['Smartphone 1'] = 'xx.xx.xx.xx',
['Smartphone 2'] = 'xx.xx.xx.xx',
['Smartphone 3'] = 'xx.xx.xx.xx',
['Smartphone 4'] = 'xx.xx.xx.xx',
['Smartphone 5'] = 'xx.xx.xx.xx'}
local timeout = 3 -- # timeouts, before a switch off will be executed
local timeoutcount = dz.data.timeoutcount -- short reference
for deviceName, ipaddr in pairs(devices_ping) do
ping_success=""
ping_success=os.execute('ping -c1 -w1 ' .. ipaddr)
if timeoutcount[deviceName] == nil then
timeoutcount[deviceName] = 0
end
if ping_success then
if timeoutcount[deviceName] == 0 then
print("ping "..deviceName.." success ")
else
if timeoutcount[deviceName] >= timeout then -- log wordt alleen weggeschreven als device offline is geweest
print("ping "..deviceName.." success, device is back.")
end
timeoutcount[deviceName] = 0 -- reset the counter
end
if dz.devices(deviceName).state == 'Off' then
dz.devices(deviceName).switchOn()
end
elseif timeoutcount[deviceName] < timeout then
timeoutcount[deviceName] = timeoutcount[deviceName] + 1
print("ping ".. deviceName .." ".. timeoutcount[deviceName] .." times failed.")
end
if timeoutcount[deviceName] >= timeout then
--print ("ping ".. deviceName .." has " ..timeoutcount[deviceName].. " timeouts, this exceeds the timeout number setting.")
if dz.devices(deviceName).state == 'On' then
dz.devices(deviceName).switchOff()
end
end
end
end
}Code: Select all
ping_success=os.execute('"ping -c1 -w1 " .. ipaddr &')Not directly with the syntax but the os.execute does not return the resultcode of the ping command but it returns the result of the &, meaning that it returns the result of sending the command to the background which always succeeds.JanvdW wrote: Monday 22 February 2021 22:48 As discussed I am changing my scripts to async versions, but I am struggling with one of my scripts. It's a script that checks presence of family members based on ip connection of their mobiles phone:
I added an ampersand to the os.execute command as follows:. It seems to work (no errors), but results always in positive values, even when devices are not connected. Is something wrong with the syntax?Code: Select all
ping_success=os.execute('"ping -c1 -w1 " .. ipaddr &')
In this case I would leave the looping to the ShellCommand. Below small bash script will do it for you and nicely shows the result in a JSON that can be processed further in dzVents when it is returned by the shellCommandResponseJanvdW wrote: Tuesday 23 February 2021 8:23 That explains what happens; thanks for your explanation. I had a look already at executeShellCommand() already, but it was not clear to me how to use it in a loop, where the executeShellCommand() is called for multiple (in my case 5) devices.
Code: Select all
#! /bin/bash
#
# bash script to loop over all parms and ping them. Parms should be a valid IPv4 or IPv6 address or hostname
#
# call like ./myPing.sh dz 192.168.192.1 192.168.192.199 8.8.8.8 noHost ::1 home.synology.me
# output will look like
#
# {"dz":0,"192.168.192.1":0,"192.168.192.199":1,"8.8.8.8":0,"noHost":2,"::1":0,"home.synology.me":0,}
#
myJSON='{'
#
# Loop over all parms
#
for ((i=1; i<=$#; i++))
do
ping -q -c1 -w1 ${!i} > /dev/null 2>&1
myJSON=$myJSON\"${!i}\":$?, # add ip + result to the JSON
done
echo ${myJSON::-1}"}" # remove last , and add }
Code: Select all
local scriptVar = 'shell pinger'
return
{
on =
{
timer =
{
'every minute',
},
shellCommandResponses =
{
scriptVar,
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = scriptVar,
},
execute = function(dz, item)
if item.isTimer then
local IPTable =
{
"dz",
"192.168.192.1",
"192.168.192.199",
"8.8.8.8",
"noHost",
"::1",
"home.synology.me",
}
dz.executeShellCommand(
{
command = '/home/pi/domoticz/scripts/myPinger.sh ' .. table.concat(IPTable,' '),
callback = scriptVar,
timeout = #IPTable -- same number of seconds as there are entries in IPTable
})
elseif item.isShellCommandResponse and item.json then
rt = item.json
for host, rc in pairs(rt) do
dz.log('Ping result of "' .. host .. '" : ' .. rc, dz.LOG_DEBUG)
end
else
dz.log('There was a problem handling the command', dz.LOG_ERROR)
dz.log(item, dz.LOG_DEBUG)
end
end
}
Code: Select all
Error: dzVents script command running longer than specified timeout(5 seconds), cancelling...Code: Select all
2021-02-23 14:20:05.736 Error: dzVents: Error: (3.1.5) shell pinger: Error parsing json to LUA table: /home/pi/domoticz/scripts/dzVents/../lua/JSON.lua:808: trailing garbage at byte 231 of: {"xx.xx.xx.xx":0,"xx.xx.xx.xx":0,"xx.xx.xx.xx":0,"xx.xx.xx.xx":1,"xx.xx.xx.xx":1}
2021-02-23 14:20:05.736 {"xx.xx.xx.xx":0,"xx.xx.xx.xx":0,"xx.xx.xx.xx":0,"xx.xx.xx.xx":1,"xx.xx.xx.xx":1,"xx.xx.xx.xx":0,"xx.xx.xx.xx":0,"xx.xx.xx.xx":0,"xx.xx.xx.xx":1,"xx.xx.xx.xx":1,}
2021-02-23 14:20:05.736 Error: dzVents: Error: (3.1.5) shell pinger: There was a problem handling the command
2021-02-23 14:20:12.657 Error: dzVents script command running longer than specified timeout(5 seconds), cancelling..Code: Select all
Error: dzVents script command running longer than specified timeout(12 seconds), cancelling...What is your build?JanvdW wrote: Tuesday 23 February 2021 16:47 I don't understand why I get an error message that timeout of 12 seconds is exceeded:. A message that I get over and over again.Code: Select all
Error: dzVents script command running longer than specified timeout(12 seconds), cancelling...
Code: Select all
timeout = #IPTable -- same number of seconds as there are entries in IPTable
Code: Select all
timeout = ( #IPTable + 10 ) -- same number of seconds as there are entries in IPTable + 10