Turn off Monitor using CLI

This is another gift to future me from present me. I made the mistake of not properly writing this down before so I had to search for the answer. The problem is, sometimes, it seems as though Plasma is not shutting off my external screens consistently. I can’t say why but I have a suspicion that it is due to a specific communication application as I can almost guarantee that it is preventing my screens from turning off. I don’t have definitive proof of this so I am not going to put it in writing.

My intent is to have a shortcut for turning off all my screens instead of just locking them and hoping that the desktop environment will do its job of turning them off. I do want to point out that when I was using Windows, both 7 and 10, I had this problem too so it is absolutely not an issue with Desktop Linux.

It is fun being able to understand how to talk to a Linux machine through the terminal using the CLI (Command Line Interface). The more you know about how to work with it, the more you will ultimately enjoy your journey in Linux. Here is my solution.

The Commands

The commands I found out there in the vastness of the world wide web lead me to this that I have tested on multiple machines. Two were running Tumbleweed with Plasma and the other Leap 15.2 with Plasma.

xset -display :0 dpms force off

The other command is to force the screen on. This is useful as I have had issues where after undocking my machine, my screen would forget to turn on. I can’t say the reason why but this could also use a Global Shortcut

xset -display :0 dpms force on

The Script

I created a little shell script for turning off my screen called screenoff.sh. I can’t say for sure how all distributions handle this but I have a bin directory in my home folder, so this is where I have chosen to place this script. ~/bin

Using nano, I created a bash script for this.

nano ~/bin/screenoff.sh

Then filled it in with this information

#!/bin/bash

sleep 1
xset -display :0 dpms force off

The purpose of the sleep 1 line is to give me a chance to get my hand away form the keyboard and mouse so I don’t inadvertently cause the desktop environment to wake the screen.

Next I made the file executable. There are many ways to do it but since we are playing in the terminal:

chmod +x ~/bin/screenoff.sh

To test this out, using krunner or open a terminal and type screenoff.sh should turn off your screen. If not, something is wrong and maybe we can figure it out…

Custom Shortcut

It is not real practical to open up krunner or a terminal just to shut off the screen when I have the power to create a custom shortcut in Plasma. Here is how to do it. First open up System Settings and choose the shortcuts module. Your system settings may look a bit different but I am sure you can figure it out. I have faith in you.

Next you have to select the “Custom Shortcuts” submodule.

At the bottom of the list there is an Edit button with a down arrow. Select that > New > Global Shortcut > Command/URL

Name it whatever makes sense for you. I chose the name “Screen Off” to make it pretty clear. Set your shortcut. I chose Meta+Alt+O.

Next, Select the Action tab and enter the path of the script you just created. In my case, it is:
~/bin/screenoff.sh

Select Apply and test it out!

Final Thoughts

Plasma is real easy to customize to your liking. I am very happy with this small modification to make my desktop experience a bit more suited to my personal taste. I don’t expect that this is a very common use case but since I know I am an edge case in much of what I do, this helps me to remember and hopefully there will be at least one person that can use or adapt this to their own case.

I am not a terminal expert so if there is any way that this can be improved, please contact me or comment below

References

Terminal Applications
https://askubuntu.com/questions/62858/turn-off-monitor-using-command-line
https://unix.stackexchange.com/questions/16815/what-does-display-0-0-actually-mean

2 thoughts on “Turn off Monitor using CLI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.