LUA os.execute('ping') not working under service

Moderator: leecollings

Post Reply
DarkAllMan
Posts: 52
Joined: Friday 23 December 2016 9:41
Target OS: Linux
Domoticz version:
Contact:

LUA os.execute('ping') not working under service

Post 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
Last edited by DarkAllMan on Thursday 27 July 2017 16:05, edited 1 time in total.
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: LUA OS Execute not working under service

Post 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:

Code: Select all

# which ping
/bin/ping
What is exit code of command (ping_success variable) ?
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
DarkAllMan
Posts: 52
Joined: Friday 23 December 2016 9:41
Target OS: Linux
Domoticz version:
Contact:

Re: LUA OS Execute not working under service

Post 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:

Code: Select all

# which ping
/bin/ping
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
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: LUA OS Execute not working under service

Post 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)
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
DarkAllMan
Posts: 52
Joined: Friday 23 December 2016 9:41
Target OS: Linux
Domoticz version:
Contact:

Re: LUA OS Execute not working under service

Post 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
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: LUA OS Execute not working under service

Post 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)
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
DarkAllMan
Posts: 52
Joined: Friday 23 December 2016 9:41
Target OS: Linux
Domoticz version:
Contact:

Re: LUA OS Execute not working under service

Post 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
DarkAllMan
Posts: 52
Joined: Friday 23 December 2016 9:41
Target OS: Linux
Domoticz version:
Contact:

Re: LUA OS Execute not working under service

Post 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
DarkAllMan
Posts: 52
Joined: Friday 23 December 2016 9:41
Target OS: Linux
Domoticz version:
Contact:

Re: LUA OS Execute not working under service

Post 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.
sassinak
Posts: 1
Joined: Wednesday 23 November 2016 20:15
Target OS: Linux
Domoticz version:
Contact:

Re: LUA os.execute('ping') not working under service

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest