0

Once the system is inactive, in 10 minutes it goes in a blank screen (but not in sleep!) and that's precisely the moment I'd like to execute a long-running script in a terminal while I go for a coffee or take a break. When I come back and "wake up" the system (although technically it's not "sleeping"), I can manually stop the running program that has been executed at the screen off.

Example of such a command:

/usr/bin/screen -S screen-off -dm my-command

There is a bunch of similar questions (Execute a script upon logout/reboot/shutdown in Ubuntu) but they address services that are executed upon the sleep and wake up and this does not suit my needs.

dizcza
  • 115

1 Answers1

1

Using dbus-monitor you can watch for the screensaver signal like this:

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
  while read x; do
    case "$x" in 
      *"boolean true"*) echo "Start my script";;
      *"boolean false"*) echo "Cancel my script";;  
    esac
  done

Replace the echo statements with whatever you need, and you should be good to go.

matigo
  • 22,138
  • 7
  • 45
  • 75
  • 1
    Doesn't this answer introduce overhead by constantly polling the system while read x? I imagined a command that is run only once (at the interrupt, speaking of polling and interrupts). – dizcza May 20 '21 at 08:31
  • @dizcza did you happen to figure out how to avoid the polling? – Constantine Apr 07 '22 at 11:44
  • @Constantine nope, I didn't. The solution above satisfied my needs. There was another sharp answer that the non-polling solution will be me hitting a keyboard shortcut each time I go for a coffee. Don't know what happened to this strikingly smart comment. – dizcza Apr 08 '22 at 08:35
  • This actually monitors the screensaver state, and not the state of the screen. – Luis A. Florit Sep 01 '23 at 22:54