How to end endless while loop?

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
DanTheManNL
Posts: 5
Joined: Sunday 20 August 2017 18:11
Target OS: Windows
Domoticz version:
Contact:

How to end endless while loop?

Post by DanTheManNL »

Hello,

I've made a bash script with an endless while loop to switch between two colours for my Limitless/Milight bulbs. I've created a scene and attached this script to the on action section.
But now how do I end this while loop? I want to create another scene which on action first stops this while loop and then does some other stuff but I can't get it to work with break or anything else.

Whenever I execute this script from my raspberry pi I can type in ctrl + c and it stops but when I use the on action mode of a scene I have to shutdown my raspberry because it keeps sending signals. :lol:

This is my bash script:

Code: Select all

echo -n -e "\x49\00\x55" >/dev/udp/xxx.xxx.xxx.xxx/8899         # Keuken ON
while :
do
echo -n -e "\x40\x20\x55" >/dev/udp/xxx.xxx.xxx.xxx/8899        # Baby Blue
sleep 0.500
echo -n -e "\x40\xC0\x55" >/dev/udp/xxx.xxx.xxx.xxx/8899        # Pink
sleep 0.500
done
echo done
Thanks in advance.
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: How to end endless while loop?

Post by nigels0 »

I use this script fragment to stop itself running more than once at a time. Should be easy to adapt to kill your looping process by putting the name of your looping process into the counter variable and seeing if it exists. You can then kill the process.

Code: Select all

count="$(ps -ef | grep -v grep |grep -c "$counter")"
if (test $count -gt  3)
then
curl "http://127.0.0.1:8080/json.htm?type=command&param=addlogmessage&message='Already running..'"
echo "Quitting..."
exit 1
fi
tozzke
Posts: 135
Joined: Friday 02 January 2015 9:22
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

Re: How to end endless while loop?

Post by tozzke »

I've also got some scripts (which take a very long time to run) which I run by pressing a group (or switch). To run, I've added the the first scripts to the on action field and the second script to the off action field:

Code: Select all

#!/bin/bash
bash -c "<PATH\YOUR ENDLESS SCRIPT>.sh" &

Code: Select all

#!/bin/bash
killall <YOUR ENDLESS SCRIPT>.sh
When you run the endless script from the on action field, Domoticz will no longer run other scripts because it's still running the endless script. Therefore I use the first script above (which takes very short to run) to be able to run a second/third/whatever script
DanTheManNL
Posts: 5
Joined: Sunday 20 August 2017 18:11
Target OS: Windows
Domoticz version:
Contact:

Re: How to end endless while loop?

Post by DanTheManNL »

@tozzke: This sounds like a good option. But when is the off action field triggert? Because you cannot close a scene only start a scene when clicking the power button of the scene.
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: How to end endless while loop?

Post by bbqkees »

DanTheManNL wrote: Friday 03 November 2017 11:25 @tozzke: This sounds like a good option. But when is the off action field triggert? Because you cannot close a scene only start a scene when clicking the power button of the scene.
Just create group instead of a scene.
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
tozzke
Posts: 135
Joined: Friday 02 January 2015 9:22
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

Re: How to end endless while loop?

Post by tozzke »

bbqkees wrote: Friday 03 November 2017 11:56
DanTheManNL wrote: Friday 03 November 2017 11:25 @tozzke: This sounds like a good option. But when is the off action field triggert? Because you cannot close a scene only start a scene when clicking the power button of the scene.
Just create group instead of a scene.
what he says ^^ :)

and I already said ;)
DanTheManNL
Posts: 5
Joined: Sunday 20 August 2017 18:11
Target OS: Windows
Domoticz version:
Contact:

Re: How to end endless while loop?

Post by DanTheManNL »

I've tried but can't get it to work.
killall <scriptname.sh> doesn't work. I can only kill it with "kill processid" but I don't know what that is because it's always different. =/

When I run it from the command line the output is:
"/home/pi/domoticz/scripts/blink-blue-pink.sh: no process found"
tozzke
Posts: 135
Joined: Friday 02 January 2015 9:22
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

Re: How to end endless while loop?

Post by tozzke »

is that 'blink-blue-pink.sh' script the one with the endless loop? or is that the one which starts the endless loop script?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests