os.execute curl - I need an error catch??

Moderator: leecollings

Post Reply
binbo
Posts: 50
Joined: Sunday 18 June 2017 21:47
Target OS: -
Domoticz version:
Contact:

os.execute curl - I need an error catch??

Post by binbo »

Hi all,

I’m using this line:

Code: Select all

os.execute ('curl -s "https://www.example.com/gateway/?user=binbo&pass=123
It works perfect - except when that site is done - or slow… then it seems to stop my lua script completing. Non-async?

How can I catch when the curl fails to allow my other parts of the script to continue running?

Thanks for the help
willemd
Posts: 650
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: os.execute curl - I need an error catch??

Post by willemd »

You can use executeShellCommand in Dzvents for asynchronous shell script execution. It will not block anything.
Here is an example.

Code: Select all

local diskused='diskused'

return {
	on = {
		timer = {
			'every minute'
		},
		shellCommandResponses =
        {
            diskused,
        },
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, item)
	    if item.isTimer then
    		domoticz.log('Timer event was triggered by ' .. item.trigger, domoticz.LOG_INFO)
    		
    		domoticz.executeShellCommand({
                command = 'df -h /media/usbdisk | grep media | awk \'{print $5}\' | awk -F% \'{print $1}\'',
                callback = diskused,
                timeout = 5,
            })
        else 
            domoticz.log('Disk used ' .. item.data, domoticz.LOG_INFO)
            domoticz.log('Disk used % ' .. tonumber(item.data), domoticz.LOG_INFO)
            if tonumber(item.data)<10 then
                domoticz.log('Enough space left. Disk used ' .. item.data, domoticz.LOG_INFO)
            else  
                domoticz.log('Watch your disk space. Disk used ' .. item.data, domoticz.LOG_INFO)
            end    
        end
	end
}

binbo
Posts: 50
Joined: Sunday 18 June 2017 21:47
Target OS: -
Domoticz version:
Contact:

Re: os.execute curl - I need an error catch??

Post by binbo »

Oh heck! That’s above my pay grade coding :lol:

I’ll PayPal you £5 for a pint if you can show me how to replace my curl execute line - for your example - using my example.com domain.

Thank you so much for reply.
willemd
Posts: 650
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: os.execute curl - I need an error catch??

Post by willemd »

Don't need compensation, thanks.

You can put the curl between quotes on the line where it shows "command =". In my example it shows a sequence of shell commands resulting in a number, i.e. disk percentage used. (you can run it at the shell prompt to see what it does)

The script first is triggered via a timer. This then launches the shell command (curl in your case) and the dzvents scripts ends. (the "then" part of the dzvents script).

The shell scripts is executed asynchronously and triggers a callback when finished, which starts the dzvents script again, but now the "else" part is executed because it was not triggered by the timer. Within the "else" part you put any handling of the output of your curl command.

Just make sure your "callback" is named consistently throughout the script ("diskused" in my case, you can leave as-is for testing, it is just a name).
User avatar
bewo
Posts: 74
Joined: Monday 13 July 2015 12:27
Target OS: Linux
Domoticz version: 2021.1
Location: Bavaria - Germany
Contact:

Re: os.execute curl - I need an error catch??

Post by bewo »

Hi binbo,

don't know if the page you would like to call is important for the rest of the script.
If not just fire the command in the background with an single & at the end. ;)

Code: Select all

os.execute('curl -s "https://www.example.com/gateway/?user=binbo&pass=123" &')
Individual projects:
Domoticz on a Intel Xeon Server | AeonLabs Z-Wave Gen.5 | RFXCOM RFXtrx433E USB | ESP-Wifi-Modules | Shellys
Wall-mounted 22" Touch Control Display (self construct) | LUA wind monitor| LUA heating control | and many many more :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest