Power button with regret option by esekeyd February 01, 2023 01:38PM |
Registered: 7 years ago Posts: 181 |
$ sudo chmod +x /root/power-off.sh $ sudo ls -lF /root/power-off.sh -rwxr-xr-x 1 root root 1793 Jan 31 13:43 /root/power-off.sh* $ sudo cat /root/power-off.sh #!/bin/bash # # Script activated by esekeyd when the power button is pressed or released # Press the power button for 5 to 10 seconds to power off the computer # States: rest, armed, active, shutdown # Usage if [ $# != 1 ]; then echo "Usage:" $( basename $0 ) "[released|pressed|timeout]" exit 0 fi # File where the state is stored state=/tmp/power-off-state # Led control (default) led_at_rest="default-on" led_when_armed="none" led_when_active="timer" # Tune in to the current box model=$( cat /sys/firmware/devicetree/base/model ) case $model in "NETGEAR MS2000 / MS2110 aka Stora" ) led="/sys/class/leds/status:blue:activity_led" ;; "NETGEAR ReadyNAS 102" ) led="/sys/class/leds/rn102:blue:pwr"; led_at_rest="none"; led_when_armed="default-on" ;; "NETGEAR ReadyNAS 104" ) led="/sys/class/leds/rn104:blue:pwr"; led_at_rest="none"; led_when_armed="default-on" ;; *) echo "$0: Please add support for this box ($model) and post it on 'https://forum.doozan.com'" echo "$0: Available LEDs are:"; ls /sys/class/leds/ | tr -s ' ' '\n' exit 1 ;; esac # The only arguments handled by this script are released, pressed and timeout case $1 in "released" ) # Shutdown or return to rest? if [ $( cat $state ) == "active" ]; then echo "shutdown" > $state echo $led_at_rest > $led/trigger if [[ ! -v DRYRUN ]]; then shutdown -h now fi else echo "rest" > $state echo $led_at_rest > $led/trigger fi ;; "pressed" ) # Arm, then wait for released or for timeout echo "armed" > $state echo $led_when_armed > $led/trigger # Spawn a child process to rerun this script again in 5 seconds with argument timeout ( cd /; exec > /dev/null 2>&1; exec < /dev/null; sleep 5; exec $0 timeout )& ;; "timeout" ) # Activate, return to rest or do nothing? if [ $( cat $state ) == "armed" ]; then echo "active" > $state; echo $led_when_active > $led/trigger # Spawn a child process to rerun this script again in 5 seconds with argument timeout ( cd /; exec > /dev/null 2>&1; exec < /dev/null; sleep 5; exec $0 timeout )& elif [ $( cat $state ) == "active" ]; then echo "rest" > $state echo $led_at_rest > $led/trigger else echo $led_at_rest > $led/trigger fi ;; esac
$ sudo apt install -y esekeyd $ sudo learnkeys /etc/esekeyd.conf /dev/input/event0 learnkeys (ESE Key Deamon 1.2.7, SVN-r63) Pres ANY (fun)key... or Ctrl-C to exit... key KEY_116 stored in config file key KEY_116 stored in config file ^C Caught signal 2, writing config file and exiting... $ sudo nano /etc/esekeyd.conf $ tail -2 /etc/esekeyd.conf KEY_116(press): /root/power-off.sh pressed KEY_116(release): /root/power-off.sh released(Substitute KEY_116 above with the name of the power button on your box.)
$ sudo perl -pi.orig -e 's/DODTIME=10/DIETIME=10/g' /etc/init.d/esekeyd $ diff /etc/init.d/esekeyd.orig /etc/init.d/esekeyd 52c52 < DODTIME=10 --- > DIETIME=10 $ sudo perl -pi.orig -e 's/START_ESEKEYD=false/START_ESEKEYD=true/g' /etc/default/esekeyd $ diff /etc/default/esekeyd.orig /etc/default/esekeyd 4c4 < START_ESEKEYD=false --- > START_ESEKEYD=true $ sudo /etc/init.d/esekeyd start Starting a multimedia keyboard daemon for Linux : esekeydESE Key Deamon 1.2.7 (SVN-r63) .Now orderly shutdown should work as described.
$ watch -n 1 cat /tmp/power-off-state
$ DRYRUN=1 /root/power-off.sh pressed $ DRYRUN=1 /root/power-off.sh released
Re: Power button with regret option by esekeyd February 01, 2023 03:44PM |
Admin Registered: 13 years ago Posts: 18,821 |
Re: Power button with regret option by esekeyd February 01, 2023 05:36PM |
Admin Registered: 13 years ago Posts: 18,821 |
Quote
Key daemon (button control)
Multimedia keyboard daemon for Linux
How to use set up button to reboot or shutdown with Key Daemon (esekeyd)
Shutdown with Power button with regret option by esekeyd