Turn off your screen programmatically

When I was running Windows, I had an icon shortcut in the icon corral that allowed me to turn off the screen, rather than waiting for the screen to time out.

You can do the same under Zorin OS:
xset -display $DISPLAY dpms force off

As soon as you wiggle your mouse, the screen will come on again.

You can trigger it by all manner of means... putting an icon shortcut into the Favorites panel, via a keyboard shortcut, etc.

I chose to use the keyboard shortcut method:

Zorin Menu > Settings > Keyboard Shortcuts > scroll to the bottom and click the '+' button.

Name: Screen Off
Command: gnome-terminal -- /bin/sh -c 'xset -display $DISPLAY dpms force off > /dev/null 2>&1'
Shortcut: Super+0

Now, I just press the Super key and the 0 key on the numeric keypad, and the screen turns off.


The reason I was researching this was I was attempting to control the update interval of conky... trying to set it to 5 seconds when the screen is on, and 300 seconds when the screen is off (to lower CPU usage to the absolute minimum)... alas, the only means I've found of doing so is pretty complicated, necessitating setting up a LUA script to inject the update interval into the data store for conky... I'm still working on it. I got the code working to get conky to sense when the screen is on or off, but it's that injection code that's giving me fits.

Eventually, I hope to get it set up so that conky pauses updating when the screen is off, then resumes once the screen comes back on.

1 Like

You could always post your code and allow us a chance to assist in finding a solution. :grin:

Here's what I've got so far:

conky.config = {
... all the other settings in the conky.config section...
  -- Load LUA script to change interval.
  lua_load = '/etc/conky/conky_interval.lua'
conky.text = [[
${if_match "  Monitor is Off" == "${execi 5 xset -q | sed -n '$p'}"}
  ${lua conky_interval.lua 300}
  ${lua conky_interval.lua 5}
... the rest of the settings in the conky.text section...

The LUA script (conky_interval.lua) is a mess... I'm still trying to get it to even work without erroring out... since I've never done LUA scripting before, I'm just casting about in the dark at this point. I've written it, crashed it, erased it and rewritten it a dozen times at least.

It should look something like this:

function conky_interval(parameter)
... not sure what to put here... something which sets update_interval = parameter...

 return parameter

winapi - Is there any way to detect the monitor state in Windows (on or off)? - Stack Overflow answer number 4 (has code included) may be something similar to what you are attempting to achieve. Otherwise, there are not too many ways to access power state unless you wrote your own driver.

No, I've already got that sorted... the xset -q | sed -n '$p' code does that.

What I'm having trouble with is injecting the new value to update_interval. If I can figure that out, conky will pick up the new value when it reloads at its current update_interval, then start updating at the newly-injected update_interval.

You may have to reference the package.loaded[] variable instead of attempting to access it directly. Lua may not be allowing the direct access of the variable. I'm using hot reload - change/update value of a local variable (Lua upvalue) - Stack Overflow as a reference.