OrangePI Club
Power button script does not work - Printable Version

+- OrangePI Club (http://orangepi.club)
+-- Forum: International (English) Forums (http://orangepi.club/forumdisplay.php?fid=3)
+--- Forum: *nix Distro (http://orangepi.club/forumdisplay.php?fid=4)
+---- Forum: RetrOrangePi v4 (http://orangepi.club/forumdisplay.php?fid=31)
+----- Forum: SUPPORT (http://orangepi.club/forumdisplay.php?fid=34)
+----- Thread: Power button script does not work (/showthread.php?tid=2414)



Power button script does not work - yopyop - 02-10-2018

Hello, 

First I want to congratulate developers for releasing ROPI 4.1, it is an amazing distro. On the other hand, I want to apologize for my bad English, it is not my native language.

Today I have been working on a script that every time is executed, it puts ROPI to sleep if ROPI is running and wakes up ROPI if it is sleeping. The idea is to read the status of the CPU inside a file: if the CPU is set to "powersave" then the script wakes up ROPI and if the CPU is set to "interactive", the script puts ROPI to sleep. 

The file that is read is "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" and the script is the next:


Code:
#!/bin/bash

set -e
set -u

filename="/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"

while IFS= read -r line; do
   if [[ $line == interactive ]] ; then
       sudo sh -c "echo 4 > /sys/class/graphics/fb0/blank"
       sudo sh -c "echo 255 > /sys/class/leds/red_led/brightness"
       sudo sh -c "echo 0 > /sys/class/leds/green_led/brightness"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
       sudo pkill -f bgmusic.py
       sudo killall -q mplayer
       sudo service acpid restart
   fi
   if [[ $line == powersave ]] ; then

       sudo sh -c "echo 0 > /sys/class/graphics/fb0/blank"
       sudo sh -c "echo 0 > /sys/class/leds/red_led/brightness"
       sudo sh -c "echo 255 > /sys/class/leds/green_led/brightness"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
       python /home/pi/RetrOrangePi/Background_Music/bgmusic.py &
   fi
done < "$filename"

This script is called "TurnOnOff.sh" and is located at "/home/pi/RetrOrangePi/Power_Button" with other power scripts.

Then I assign this script to the OrangePi power button, modifying the next file "/etc/acpi/events/button_power":

Code:
event=button/power
action=/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh

if you run this script on a shell with a SSH client (I use putty), it works perfectly and flawlessly (even when ROPI is sleeping) but when the power button of the OrangePi is pressed, the script does not work, it looks like it cannot enter in the while loop.  Angry

I still don´t understand why when ROPI is asleep, the loop fails, and now i am running out of ideas.

Can someone help me to solve this issue?

All help is highly appreciated.  Blush


RE: Power button script does not work - alexkidd - 02-12-2018

I'm traveling at the moment, I'll try when I get home in a couple of days


RE: Power button script does not work - brains74 - 02-16-2018

(02-10-2018, 01:47 AM)yopyop Wrote: Hello, 

First I want to congratulate developers for releasing ROPI 4.1, it is an amazing distro. On the other hand, I want to apologize for my bad English, it is not my native language.

Today I have been working on a script that every time is executed, it puts ROPI to sleep if ROPI is running and wakes up ROPI if it is sleeping. The idea is to read the status of the CPU inside a file: if the CPU is set to "powersave" then the script wakes up ROPI and if the CPU is set to "interactive", the script puts ROPI to sleep. 

The file that is read is "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" and the script is the next:


Code:
#!/bin/bash

set -e
set -u

filename="/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"

while IFS= read -r line; do
   if [[ $line == interactive ]] ; then
       sudo sh -c "echo 4 > /sys/class/graphics/fb0/blank"
       sudo sh -c "echo 255 > /sys/class/leds/red_led/brightness"
       sudo sh -c "echo 0 > /sys/class/leds/green_led/brightness"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
       sudo sh -c "echo powersave > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
       sudo pkill -f bgmusic.py
       sudo killall -q mplayer
       sudo service acpid restart
   fi
   if [[ $line == powersave ]] ; then

       sudo sh -c "echo 0 > /sys/class/graphics/fb0/blank"
       sudo sh -c "echo 0 > /sys/class/leds/red_led/brightness"
       sudo sh -c "echo 255 > /sys/class/leds/green_led/brightness"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
       sudo sh -c "echo interactive > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
       python /home/pi/RetrOrangePi/Background_Music/bgmusic.py &
   fi
done < "$filename"

This script is called "TurnOnOff.sh" and is located at "/home/pi/RetrOrangePi/Power_Button" with other power scripts.

Then I assign this script to the OrangePi power button, modifying the next file "/etc/acpi/events/button_power":

Code:
event=button/power
action=/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh

if you run this script on a shell with a SSH client (I use putty), it works perfectly and flawlessly (even when ROPI is sleeping) but when the power button of the OrangePi is pressed, the script does not work, it looks like it cannot enter in the while loop.  Angry

I still don´t understand why when ROPI is asleep, the loop fails, and now i am running out of ideas.

Can someone help me to solve this issue?

All help is highly appreciated.  Blush

Hi, the script permission are set for user pi and group pi ?


RE: Power button script does not work - yopyop - 02-17-2018

yes, the permissions were set to be freely used by any user with chmod.


I found a solution to the problem, if you try to to run next command:

Code:
/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh


The shell gives the next error code:

Code:
/home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh: 9: /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh: [[: not found
/home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh: 21: /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh: [[: not found

But if you run the next code ('action=' at the beginning):

Code:
action=/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh

Then the script runs nicely.

So I change the next file "/etc/acpi/events/button_power":

Code:
event=button/power
action=action=/bin/sh /home/pi/RetrOrangePi/Power_Button/TurnOnOff.sh

The only difference between this version and the last one is the extra '=action' code.

With this modification, the script runs, even using the Orangepi button. So it is nice.  Cool Cool

With this script, the Orangepi button is a switch button to wake up the ROPI or put it to sleep.

My only problem now is that every time I sent the ROPI to sleep through the emulationstation menu, the file "/etc/acpi/events/button_power" is changed automatically. 

How can I change what the sleep menu option does in emulationstation?

Thanks a lot for the support!


RE: Power button script does not work - alexkidd - 02-17-2018

(02-17-2018, 04:02 AM)yopyop Wrote: My only problem now is that every time I sent the ROPI to sleep through the emulationstation menu, the file "/etc/acpi/events/button_power" is changed automatically. 

How can I change what the sleep menu option does in emulationstation?

Thanks a lot for the support!

edit /home/pi/RetrOrangePi/Power_Button/Sleep_mode.sh and remove the first line (that changes acpi/button_power) otherwise you would have to recompile EmulationStation code.