Script for turning off monitor

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ZeRenXX
Level 1
Level 1
Posts: 9
Joined: Sun Aug 30, 2020 1:50 am

Script for turning off monitor

Post by ZeRenXX »

Hi, somewhere on internet I found script for tuning off monitor by creating custom shortcut:

Code: Select all

#!/usr/bin/python

import time

import subprocess

from Xlib import X

from Xlib.display import Display

display = Display(':0')

root = display.screen().root

root.grab_pointer(True,

       X.ButtonPressMask | X.ButtonReleaseMask | X.PointerMotionMask,

       X.GrabModeAsync, X.GrabModeAsync, 0, 0, X.CurrentTime)

root.grab_keyboard(True,

       X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)

subprocess.call('xset dpms force off'.split())

p = subprocess.Popen('gnome-screensaver-command -i'.split())

time.sleep(1)

while True:

   print display.next_event()

   p.terminate()

   break
in Linux Mint 19.3 it worked great, but when I upgraded to LM 20 it doesnt working anymore

when I press the shortcut it just does nothing

did something change?

btw it would be great if LM had shortcut for turning off monitor coded in
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
deck_luck
Level 7
Level 7
Posts: 1577
Joined: Mon May 27, 2019 6:57 pm
Location: R-4808 North

Re: Script for turning off monitor

Post by deck_luck »

You should probably try running the python program from the command line and check for errors.

Linux Mint 20 is based on Ubuntu 20.4. The Ubuntu 20.4 release notice indicates the following:

Python3 by default
In 20.04 LTS, the python included in the base system is Python 3.8. Python 2.7 has been moved to universe and is not included by default in any new installs.

Remaining packages in Ubuntu which require Python 2.7 have been updated to use /usr/bin/python2 as their interpreter, and /usr/bin/python is not present by default on any new installs. On systems upgraded from previous releases, /usr/bin/python will continue to point to python2 for compatibility. Users who require /usr/bin/python for compatibility on newly-installed systems are encouraged to install the python-is-python3 package, for a /usr/bin/python pointing to python3 instead.

Due to this transition the legacy python and python-minimal packages might be removed during an upgrade, being replaced by the python2 and python2-minimal packages as dependencies of the python-is-python2 package.


Reference:
https://wiki.ubuntu.com/FocalFossa/Rele ... by_default
🐧Linux Mint 20.3 XFCE (UEFI - Secure Boot Enabled) dual boot with Windows 11

Give a friend a fish, and you feed them for a day. Teach a friend how to fish, and you feed them for a lifetime. ✝️
chrm
Level 1
Level 1
Posts: 17
Joined: Thu Aug 20, 2020 10:01 am

Re: Script for turning off monitor

Post by chrm »

You can use xrandr for that. First run

Code: Select all

xrandr -q
to get the name for the monitor you want to turn off. Then run

Code: Select all

xrandr --output <name> --off
To turn the monitor back on use

Code: Select all

xrandr --output <name> --auto
ZeRenXX
Level 1
Level 1
Posts: 9
Joined: Sun Aug 30, 2020 1:50 am

Re: Script for turning off monitor

Post by ZeRenXX »

chrm wrote: Mon Aug 31, 2020 9:04 am Y to get the name for the monitor you want to turn off. Then run

Code: Select all

xrandr --output <name> --off
this works, but it turn it off completely, I cant turn it on after ( I have one monitor), need to restart PC

I was using the script to turn off monitor and then I moved mouse to turn it back on

is there a command for "soft" turn off?
chrm
Level 1
Level 1
Posts: 17
Joined: Thu Aug 20, 2020 10:01 am

Re: Script for turning off monitor

Post by chrm »

That sounds more like you want to turn on the screen saver, and not turn off the monitor. Xrandr probably can't do that.

You could use Xrandr if you make two keybindings in your window manager. One keybinding for off and one for on.
ZeRenXX
Level 1
Level 1
Posts: 9
Joined: Sun Aug 30, 2020 1:50 am

Re: Script for turning off monitor

Post by ZeRenXX »

chrm wrote: Thu Sep 03, 2020 4:22 pm You could use Xrandr if you make two keybindings in your window manager. One keybinding for off and one for on.
thx dude, I made custom shortcuts with this command:

xrandr --output HDMI-A-0 --off
xrandr --output HDMI-A-0 --auto

and works great
tuxoneseven

Re: Script for turning off monitor

Post by tuxoneseven »

this works, but it turn it off completely, I cant turn it on after ( I have one monitor), need to restart PC
If the terminal is open, you can also type the xrandr command while the screen is off to turn it on.
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Script for turning off monitor

Post by Flemur »

ZeRenXX wrote: Thu Sep 03, 2020 1:16 pm
chrm wrote: Mon Aug 31, 2020 9:04 am Y to get the name for the monitor you want to turn off. Then run

Code: Select all

xrandr --output <name> --off
this works, but it turn it off completely, I cant turn it on after ( I have one monitor), need to restart PC

I was using the script to turn off monitor and then I moved mouse to turn it back on

is there a command for "soft" turn off?
Try this (a line from your original overly complicated script)

Code: Select all

sleep 1; xset dpms force off
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Script for turning off monitor

Post by Termy »

ZeRenXX wrote: Thu Sep 03, 2020 1:16 pm is there a command for "soft" turn off?
I would use this: xset dpms force off

It doesn't really turn it off, but it's "soft", I guess. Pressing a key or likely moving the mouse should bring the monitor back to life, but if not, type in (blindly): xset dpms force on
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”