How To Switch On Your Black Screen: Shortcut & Script

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
User avatar
sebastjava
Level 5
Level 5
Posts: 681
Joined: Wed Jun 28, 2017 8:01 pm
Location: Montréal, Québec, Canada
Contact:

How To Switch On Your Black Screen: Shortcut & Script

Post by sebastjava »

Do you get a black screen when your computer resumes from suspend? The screen is off and it is damn awkward. If you are lucky enough to know which command to use to turn it back on, you can't do that, because you can't see anything! This tutorial will show you how to solve this. We can have a simple keyboard shortcut to turn on the screen, or we can have it done automatically.

I have a little Asus Eee PC X101CH. I had this problem with Linux Mint Xfce 18.2 installed. I don't have this problem anymore since i installed Linux Mint MATE 18.2. But you can try what follows if you are stuck with a black screen on resume from suspend. Like when you simply close the lid and re-open it.


FIRST, MAKE A SIMPLE KEYBOARD SHORTCUT

Get your monitor's name by typing xrandr --listactivemonitors in your terminal. Example:

Code: Select all

xrandr --listactivemonitors
Monitors: 1
 0: +*LVDS-1 1024/223x600/125+0+0  LVDS-1
In this example, the monitor's name is LVDS-1. Remember your monitor's name.
  1. Go to: Start Menu > Preferences > Keyboard Shortcuts
  2. Click Add
  3. Name it "Screen ON" and enter this command: /usr/bin/xrandr --output LVDS-1 --auto
    (Change "LVDS-1" to your monitor's name.)
  4. Click Apply
  5. Click over "Disabled" and press Ctrl+F7
  6. Close
Now, test it. Save all your opened files and close everything, because if it does not work you will have to restart your computer. Ready? Open a terminal and turn off the screen with this command: xrandr --output LVDS-1 --off. (Change "LVDS-1" to your monitor's name.) Pressing Ctrl+F7 should get you out of trouble.

That was all you needed. But if you want to make things more pleasant, do what follows.


GET YOUR SCREEN TURNED ON AUTOMATICALLY
  1. Copy the script named "01-SCREEN-ON.SH" listed at the bottom of this page.
  2. Paste it into a simple text editor, not into a big word processor.
  3. Change "USERNAME" to your user name.
  4. Get your monitor's name by typing xrandr --listactivemonitors in your terminal. Example:

    Code: Select all

    username@computername ~ $ xrandr --listactivemonitors
    Monitors: 1
     0: +*LVDS-1 1024/223x600/125+0+0  LVDS-1
    In this example, the monitor's name is "LVDS-1".
  5. In your script, change every "LVDS-1" to your monitor's name.
  6. Save your script in plain text format and name it with ".sh" extension, like this: "01-screen-on.sh".
  7. Open this folder as administrator (right-click to do so): /usr/lib/pm-utils/sleep.d/
  8. Place a copy of your "01-screen-on.sh" script in this folder.
  9. In your file manager, in that same folder, right-click on your script's icon to edit properties. Select "Permissions" tab and click on "Execute" to allow executing file as program.
  10. Restart your computer.
Test it! Close and re-open the lid. If the screen is not switched on automatically, you still have your handy keyboard shortcut to get you out of trouble.


TROUBLESHOOTING:
  1. /usr/lib/pm-utils/sleep.d/ might not be the right folder. There is a folder where the system will execute all the scripts on resume from suspend, but that folder might be somewhere else depending on your system's version. Search the web on this subject.
  2. Make sure you have /usr/share/sounds/linuxmint-login.wav and /usr/share/sounds/linuxmint-gdm.wav files. If not, replace them with some other sound. Those sounds are required. They help you know what is going on and they insert a required delay.
  3. Make sure you have all the commands installed: sleep, xrandr & aplay.
  4. Read all this tutorial again.
01-SCREEN-ON.SH

Code: Select all

#!/bin/bash
# This script should power on your screen on resume from suspend.
# It was used with Linux Mint Xfce 18.2 on my Asus Eee PC X101CH.
#
# Lots of things are happening on resume from suspend. Screen is first
# turned on and back off. So this scripts starts with a sleep, then
# goes into a loop: sleep, check, exit or try to switch on, loop...
# Created by SebastJava with some inputs from dro0g, sdennie & swissz:
# https://ubuntuforums.org/showthread.php?t=1190894

case "$1" in
resume)
	sleep 3
	export DISPLAY=:0
	export XAUTHORITY=/home/USERNAME/.Xauthority

	for var in 1 2 3 4 5 6 7 8 9 10; do
		sleep 1
		if (xrandr --listactivemonitors|grep LVDS-1); then
			aplay /usr/share/sounds/linuxmint-login.wav
			echo "The monitor is active."; exit 0
		else
			xrandr --output LVDS-1 --auto --brightness 1.0
			aplay /usr/share/sounds/linuxmint-gdm.wav
			echo "XRANDR is trying to activate the monitor..."
		fi
	done
;;
esac
exit 0
The future Linux Mint Forums is here.
Self-Appointed Benevolent Designer on Linux Mint Cinnamon.
Image
User avatar
rudolf
Level 1
Level 1
Posts: 2
Joined: Sun Jul 12, 2020 4:16 pm

Re: How To Switch On Your Black Screen: Shortcut & Script

Post by rudolf »

First of all, huge thanks to @sebastjava for posting this article.
I just created a script. For the simplicity, it is not all-compliant. Just tested on my box, so not sure if it works. Also involves a bit bigger one-liner with automatic screen name detection. You can try and temporarily remove the #TEST# comments if you want to test. There, you are going to have 5 seconds to test your hotkeys. If you have a problem, let me know here.

Code: Select all

#!/bin/sh
# screen_on.sh
# by rudolf  (2022)
# based on: https://forums.linuxmint.com/viewtopic.php?t=263558


monitor=$(/usr/bin/xrandr --listactivemonitors | awk ' /0:/ {print $NF}')
echo "monitor=${monitor}"
#TEST# /usr/bin/xrandr --output "${monitor}" --off
#TEST# sleep 5
/usr/bin/xrandr --output "${monitor}" --auto

# Alternative one-liner:
# /usr/bin/xrandr \
#   --output \
#     "$(/usr/bin/xrandr --listactivemonitors | awk ' /0:/ {print $NF}')" \
#   --auto

 
* note, the one-liner is on 4 lines for readability. the backslash \ character tells the shell that the command continues on the line. *
But here you are:

Code: Select all

/usr/bin/xrandr --output "$(/usr/bin/xrandr --listactivemonitors | awk ' /0:/ {print $NF}')" --auto
I have also found a little bit different naming scheme in the sleep.d directory. (You may need to adjust the source directory.)

Code: Select all

  sudo cp ~/scripts/screen_on.sh /usr/lib/pm-utils/sleep.d/01screen_on.sh 
I was having another screen name (eDP), so I thought I will just make it a bit more dynamic! B-)
I hope it helps!
--
Rudolf
Post Reply

Return to “Tutorials”