Page 1 of 1

While Loop

Posted: Thu Feb 19, 2015 7:05 pm
by expandables
Hi i have a bash script with a 1 second while loop running at every boot. I want to know will a while loop script slow down the raspberry pi?

Re: While Loop

Posted: Thu Feb 19, 2015 8:58 pm
by rpdom
If you mean you have a loop with a "sleep 1" in it, that won't load the Pi at all.

If you have a loop where it is constantly checking the time to see if a second has passed, then that will use a lot of CPU.

Show the code and we will tell :)

Re: While Loop

Posted: Thu Feb 19, 2015 9:36 pm
by expandables
rpdom wrote:If you mean you have a loop with a "sleep 1" in it, that won't load the Pi at all.

If you have a loop where it is constantly checking the time to see if a second has passed, then that will use a lot of CPU.

Show the code and we will tell :)
I run this script @reboot using crontab.

Code: Select all

while [ 1 ];
do
 sudo sh /home/pi/exitfix.sh & /home/pi/exitfix2.sh
  sleep 1;
done
It executes two scripts the first one

Code: Select all

#!/bin/bash
  find /home/pi/.kodi/temp/kodi.log  -type f -exec egrep -w "Attempted to remove window 10013" {} \;  > /home/pi/exitfix2.txt
finds a string in a log file and put it in a text file and the next script

Code: Select all

exitfix=$(cat /home/pi/exitfix2.txt| awk 'NR==1' | tail -c 6)
     if [ $exitfix = "exist" ]; then
      sudo killall /usr/lib/kodi/kodi.bin && sudo rm -r /home/pi/exitfix2.txt /home/pi/.kodi/temp/kodi.log
fi
looks for a specific word and if it find the word it kills a process and and deletes the text file.
I do this so when i run Kodi on Raspbian and I exit, it exits automatically.
I also ran the script manually in a terminal and when it deletes the text file it loops to this

Code: Select all

/home/pi/exitfix2.sh: 2: [: =: unexpected operator
find: `/home/pi/.kodi/temp/kodi.log': No such file or directory
I want to know will this use a lot of CPU?

Re: While Loop

Posted: Thu Feb 19, 2015 9:50 pm
by PeterO
I think you probably want

Code: Select all

sudo sh /home/pi/exitfix.sh && /home/pi/exitfix2.sh
Then the second script will only be run if the first one returns "true"

PeterO

Re: While Loop

Posted: Thu Feb 19, 2015 10:00 pm
by expandables
PeterO wrote:I think you probably want

Code: Select all

sudo sh /home/pi/exitfix.sh && /home/pi/exitfix2.sh
Then the second script will only be run if the first one returns "true"

PeterO
Ok thanks I will put that and see.. :mrgreen: