Welcome! Log In Create A New Profile

Advanced

Power button with regret option by esekeyd

Posted by tme 
tme
Power button with regret option by esekeyd
February 01, 2023 01:38PM
I want to orderly shut down my boxes by pressing the power button for 5+ seconds. Sometimes, though, I realize that there is this One More Thing I want to do first, so holding it down for 10+ seconds should do nothing. I also want to see when to release the button. Attached the bash script 'power-off.sh' that, together with 'esekeyd', does the trick. It is tested on Netgear Stora and Netgear ReadyNAS RN102/RN104. It should work with other Linux boxes by just adding 3 new lines to the first case statement.

Copy the script to '/root' and make it executable:
$ 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

Install 'esekeyd', press and release the power button while running 'learnkeys' and modify the 2 last lines of '/etc/esekeys.conf':
$ 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.)

Fix a flaw, enable and start the 'esekeyd' deamon:
$ 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.

To debug or further develop the script, use 'ssh' to open 2 terminal windows to the box:
$ watch -n 1 cat /tmp/power-off-state
$ DRYRUN=1 /root/power-off.sh pressed
$ DRYRUN=1 /root/power-off.sh released

Regards,
Trond Melen

EDIT: Fixed typos in text.
TIDE



Edited 2 time(s). Last edit at 02/01/2023 01:50PM by tme.
Attachments:
open | download - power-off.sh (2.1 KB)
Re: Power button with regret option by esekeyd
February 01, 2023 03:44PM
Hi Trond,

Very nice! I'll add this to the Wiki. Also it's a good reference for esekeyd tutorial, since the key names are an issue that make it seems esekeyd is broken.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: Power button with regret option by esekeyd
February 01, 2023 05:36PM
In the Wiki.

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

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: