Page 1 of 1

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

Posted: Saturday 26 November 2022 13:49
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

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

Posted: Saturday 26 November 2022 17:15
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
}


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

Posted: Saturday 26 November 2022 17:19
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.

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

Posted: Saturday 26 November 2022 17:52
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).

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

Posted: Friday 30 December 2022 15:02
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" &')