2

Acquired a great Samsung HDMI TV/monitor that doesn't go into standby when the signal is dropped. I've acquired a Pulse Eight CEC command injector, and worked out how to turn the monitor off/on from the command line.

The problem is that I don't know where the most reliable/future proof/Ubuntu/Gnome way of causing those commands to execute is - What I've looked into so far:

systemd

Seems to have targets for when a user is logged in, and for when the graphical system comes up, but not before the login.

acpid

Catching the screen sleep/wake mechanism here sounds perfect, but I can't seem to find a way to do it.

gdm3

Seems to support adding scripts in just about every case OTHER than on keyboard activity.

xss-lock

Tried using this with a script to sleep the monitor when it runs, and catch the SIGHUP to issue a wake command - but it didn't seem to work reliably.


I found this from 2015: How to run scripts on screen sleep / wake

... but just about every option above feels like a better solution than looping a background task to scrape log traffic constantly or takes @wait/2 seconds to come up after a key is pressed. Is that the best solution available?

Zanna
  • 72,312
Unstuck
  • 56

1 Answers1

1

I use below script to turn off the screen after 1 minute of idleness. Code doesn't belong to me, I just modified it to my needs

You need to install xprintidle

sudo apt install xprintidle
cd ~
mkdir myscripts
cd myscripts

copy and paste the below code into terminal to create a bash script

cat>turnoffscreen.sh<<'EOF'
#!/bin/sh
# Wanted trigger timeout in milliseconds.
IDLE_TIME=$((1*60*1000))
IDLE_WARN=$((((1*60*1000))-((10000))))
# Sequence to execute when timeout triggers.
trigger_cmd() {

   # put your code here, the below command just an example
   xset s blank ; sleep 1 ; xset s activate 

}

sleep_time=$IDLE_TIME
triggered=false

# ceil() instead of floor()
while sleep $(((sleep_time+999)/1000)); do
    idle=$(xprintidle)

if [ $idle -ge $IDLE_WARN ]; then
    notify-send --icon=info "Turning off the screen ..."
    sleep 10
    idle_check=$(xprintidle)
       if [ $idle_check -ge $idle  ]; then
        if ! $triggered; then
                 trigger_cmd
                 triggered=true
                 sleep_time=$IDLE_TIME
        fi
     fi


    else

        triggered=false

        # Give 100 ms buffer to avoid frantic loops shortly before triggers.
        sleep_time=$((IDLE_TIME-idle+100))
    fi
done
EOF

then make it executable

chmod +x turnoffscreen.sh

Add it to startup applications list

gnome-session-properties
kenn
  • 5,232