I am trying to use switch for executing a script(Let say script_a). For that i wrote a if condition in another script(Let say script_b). script_b is always running when rPi is on. While input of gpio is 1, script_a must be executed else script_b must be repeated. Here is my code:
if [ "$(gpio -g read 18)" = "1" ] then ./script_a.sh& else sleep 0.1 ./script_b.sh fi
if [ "$(gpio -g read 18)" = "0" ] then ./script_b.sh& else sleep 0.1 ./script_a.sh fi
It works, however cpu usage is very high while gpio 18 is 0 in other words while script_b is repeating itself. How can i reduce cpu usage?
note that: i am not an expert with coding. Thank You
So i decided to use if condition in while loop using only one script. I am trying to execute some processes while gpio 18 is 1 else do nothing. And i want to run that while loop infinitely. Here is my new code:
while [ "$(gpio -g read 18)" = "0" ] do sleep 0.1 done while [ "$(gpio -g read 18)" = "1" ] do #some processes are here done
however it is exit while loop if i change switch 1 to 0.
I added these lines at the and of the second while loop like that:
while [ "$(gpio -g read 18)" = "1" ] do #some processes are here if [ "$(gpio -g read 18)" = "0" ] then nohup ./stt.sh exit 0 fi done
And it is worked. But still cpu usage very high also there is only 20 mb free ram while this script is working. I am still open for suggestions. Are there anyway to reduce cpu and ram usage while doing my intended process.(By the way i am using
htop for viewing cpu usage and
free -h -s 1 for ram usage.)