Page 1 of 1
How to end endless while loop?
Posted: Wednesday 01 November 2017 23:01
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.
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.
Re: How to end endless while loop?
Posted: Thursday 02 November 2017 2:01
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¶m=addlogmessage&message='Already running..'"
echo "Quitting..."
exit 1
fi
Re: How to end endless while loop?
Posted: Thursday 02 November 2017 5:43
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
Re: How to end endless while loop?
Posted: Friday 03 November 2017 11:25
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.
Re: How to end endless while loop?
Posted: Friday 03 November 2017 11:56
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.
Re: How to end endless while loop?
Posted: Friday 03 November 2017 20:32
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 
Re: How to end endless while loop?
Posted: Wednesday 08 November 2017 16:56
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"
Re: How to end endless while loop?
Posted: Friday 10 November 2017 17:25
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?