User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

While Loop

Thu Feb 19, 2015 7:05 pm

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?
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: While Loop

Thu Feb 19, 2015 8:58 pm

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 :)

User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Re: While Loop

Thu Feb 19, 2015 9:36 pm

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?
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: While Loop

Thu Feb 19, 2015 9:50 pm

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Re: While Loop

Thu Feb 19, 2015 10:00 pm

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:
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

Return to “Beginners”