Score:2

Acpid can't execute scripts

cn flag

I have an old laptop and I installed Ubuntu 20.04 LTS Server edition (no GUI, just CLI). It managed via SSH so its display is unnecessary. I would like to turn off the display when I close the lid, and turn on again if I open it.

I've successfully configured acpid to manage the display.

In /etc/acpi/events I created a file:

event=button/lid.*
action=/etc/acpi/lid.sh

The script:

#!/bin/sh

grep -q closed /proc/acpi/button/lid/LID/state
if [ $? = 0 ]
then
    # close action
    setterm --blank force
else
    # open action
    setterm --blank poke
fi

If I run this script manually, it works perfectly, however it seems that acpi can't run the script:

$ sudo journalctl -u acpid -f

...
Jul 04 20:03:01 server acpid[1179]: received input layer event "button/lid LID close"
Jul 04 20:03:01 server acpid[1179]: rule from /etc/acpi/events/lid matched
Jul 04 20:03:01 server acpid[1353]: executing action "/etc/acpi/lid.sh"
Jul 04 20:03:01 server acpid[1179]: action exited with status 1
Jul 04 20:03:01 server acpid[1179]: 1 total rule matched
Jul 04 20:03:01 server acpid[1179]: completed input layer event "button/lid LID close"
...

Error: action exited with status 1 (not 0)

If i know right, exit code 1 is a permission issue (operation not permitted). I don't know why is it "not permitted", because I don't need to use sudo to run this script.

hr flag
Do you have a resource that says this should be possible using `setterm` from an acpid action? My understanding is that `setterm` needs to be run inside a terminal. I wonder if the exit status 1 is actually `setterm` telling you that it's not connected to a terminal (or doesn't recognize the terminal type), rather than an EPERM error?
Score:0
cn flag

So finally I figured out what was the problem.

As steeldriver mentioned, setterm needs to be run inside a terminal (in front of the laptop), and in my script it wasn't connected to a real terminal and setterm could change nothing.

According to this post, we need to redirect the output or input to/from the console.

So I modified my script:

#!/bin/sh

grep -q closed /proc/acpi/button/lid/LID/state
if [ $? = 0 ]
then
    # close action
    setterm -term linux --blank force </dev/tty1
else
    # open action
    setterm -term linux --blank poke </dev/tty1
fi

I tested this script and it's work like a charm ;)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.