Page 1 of 1
LUA os.execute('ping') not working under service
Posted: Tuesday 25 July 2017 11:29
by DarkAllMan
I have a problem that
Code: Select all
ping_success=os.execute('ping -c1 -w1 ' .. ip)
is no longer working when I run Domoticz as a service.
Running manually (./domoticz) it does execute the ping.
The service is configured like this:
Code: Select all
[Unit]
Description=domoticz_service
After=network.target
[Service]
User=username
Group=username
ExecStart=/home/username/domoticz/domoticz -www 8080
WorkingDirectory=/home/username/domoticz
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
Restart=on-failure
RestartSec=1m
KillMode=process
#StandardOutput=null
[Install]
WantedBy=multi-user.target
Re: LUA OS Execute not working under service
Posted: Tuesday 25 July 2017 11:56
by mivo
Hi,
It might be problem of permissions, or path to ping.
When you try to start Domoticz from command line, are you logged in as same user as configured in Systemd unit ?
Try to call ping command with full path. Check where it resides and change command accordingly:
What is exit code of command (ping_success variable) ?
Re: LUA OS Execute not working under service
Posted: Tuesday 25 July 2017 12:38
by DarkAllMan
mivo wrote:Hi,
It might be problem of permissions, or path to ping.
When you try to start Domoticz from command line, are you logged in as same user as configured in Systemd unit ?
Try to call ping command with full path. Check where it resides and change command accordingly:
What is exit code of command (ping_success variable) ?
Code: Select all
function DevicePing(ip, device)
ping_success=""
ping_success=os.execute('/bin/ping -c1 -w1 ' .. ip)
if ping_success then
print("ping success " ..device)
DeviceOnOff('On',device)
else
print("ping fail " ..device)
DeviceOnOff('Off',device)
end
end
ping_success result = NIL
Re: LUA OS Execute not working under service
Posted: Tuesday 25 July 2017 14:23
by mivo
Huh, os.execute function returns more values... Try to catch them like this:
Code: Select all
local a,b,c=os.execute('ping...')
print(a,b,c)
Re: LUA OS Execute not working under service
Posted: Tuesday 25 July 2017 15:00
by DarkAllMan
mivo wrote:Huh, os.execute function returns more values... Try to catch them like this:
Code: Select all
local a,b,c=os.execute('ping...')
print(a,b,c)
As a service:
Code: Select all
2017-07-25 15:15:49.584 dzVents: A: nil
2017-07-25 15:15:49.584 dzVents: b: exit
2017-07-25 15:15:49.585 dzVents: C: 2
Manually started:
Code: Select all
2017-07-25 15:16:29.567 LUA: A: true
2017-07-25 15:16:29.567 LUA: b: exit
2017-07-25 15:16:29.567 LUA: C: 0
Re: LUA OS Execute not working under service
Posted: Tuesday 25 July 2017 17:25
by mivo
At least some return code
It looks like
ping is called, but from unknown reason it does not succeeds. You can try to catch output of subprocess - not so intuitive in LUA, must be done like this:
Code: Select all
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
local ex1, ex2, ex3 = f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s, ex1, ex2, ex3
end
local a,b,c,d=os.capture('ping ... 2>&1')
print(a,b,c,d)
Re: LUA OS Execute not working under service
Posted: Wednesday 26 July 2017 9:19
by DarkAllMan
mivo wrote:At least some return code
It looks like
ping is called, but from unknown reason it does not succeeds. You can try to catch output of subprocess - not so intuitive in LUA, must be done like this:
Code: Select all
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
local ex1, ex2, ex3 = f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s, ex1, ex2, ex3
end
local a,b,c,d=os.capture('ping ... 2>&1')
print(a,b,c,d)
Result:
Code: Select all
2017-07-26 09:18:44.366 dzVents: A:
2017-07-26 09:18:44.367 dzVents: b: nil
2017-07-26 09:18:44.367 dzVents: C: exit
2017-07-26 09:18:44.367 dzVents: D: 2
Re: LUA OS Execute not working under service
Posted: Wednesday 26 July 2017 13:30
by DarkAllMan
And I'm no further..... can't figure out why it's not working when running domoticz as a service.....
When I try this:
Code: Select all
local a,b,c,d=os.capture('date "+%H:%M:%S %d/%m/%y"')
print('A: ' .. tostring(a))
print('B: ' .. tostring(b))
print('C: ' .. tostring(c))
print('D: ' .. tostring(d))
The result is:
Code: Select all
2017-07-26 13:35:31.637 LUA: A: 13:35:31 26/07/17
2017-07-26 13:35:31.637 LUA: B: true
2017-07-26 13:35:31.637 LUA: C: exit
2017-07-26 13:35:31.638 LUA: D: 0
That works... just not PING
Re: LUA OS Execute not working under service
Posted: Thursday 27 July 2017 9:37
by DarkAllMan
After adding some extra logging I found out the ping from LUA is generating the following error:
Code: Select all
ping: icmp open socket: Operation not permitted
Checking why...
Ping permissions are ok:
Code: Select all
-rwsr-xr-x 1 root root 44168 May 7 2014 /bin/ping
When using a
systemd service that pings, also no issues.
Re: LUA os.execute('ping') not working under service
Posted: Friday 18 January 2019 8:31
by sassinak
Not sure if anyone found a solution to this issue, but I encountered it also.
It is caused by systemd limiting the capabilities of the domoticz process.
Easily corrected by changing the systemd service file for domoticz (/etc/systemd/system/domoticz.service) and changing the following line
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
to
CapabilityBoundingSet=CAP_NET_BIND_SERVICE CAP_NET_RAW
You will need to run
systemctl daemon-reload
systemctl restart domoticz.service