Ever battled a stubborn external display that refuses to wake up after you log in to your Linux machine? You’re not alone. This common annoyance can disrupt your workflow and leave you staring at a blank screen. But fear not, multi-monitor warriors! Here’s a solution that automates the wake-up process, bringing your external display back to life seamlessly.
The Script: Defining Your Display Paradise Link to heading
First, we’ll craft a script that dictates your ideal display configuration. Open your terminal and use your preferred text editor (like nano or vim) to create a new file named /opt/wakeup-display.sh
with the following content:
#!/bin/sh
xrandr --output HDMI-2 --off
sleep 0.5
xrandr --output HDMI-2 --primary --mode 2560x1440 --pos 1920x0 --rotate normal --panning 0x0
xrandr --output eDP-1 --mode 1920x1080 --pos 0x800 --rotate normal
This script utilizes the xrandr
command to configure your displays. Make sure to:
- Replace
eDP-1
andHDMI-2
with the actual names of your displays (more on finding these names later). - Adjust the resolutions, positions (
--pos
), and rotations (--rotate
) to match your setup.
Once you’ve customized the script, grant it executable permissions using this command in your terminal:
chmod +x /opt/wakeup-display.sh
Identifying Your Display’s Secret Code Link to heading
Now, we need to tell the system when to execute this script. Enter udev
: a powerful tool that manages devices when they’re connected or removed. We’ll create a udev rule that triggers our script whenever the external display wakes up.
First, uncover your monitor’s device ID. Open your terminal and run:
udevadm monitor -u
This command displays a continuous stream of information about connected devices. Look for entries related to your display (e.g., containing “drm”). Note down the value for KERNEL
(it might look something like card1
).
Crafting the Magic Rule: Automation to the Rescue Link to heading
With the device ID in hand, create a new file named 99-wakeup-display.rules inside the directory /usr/lib/udev/rules.d/. Use your text editor and add the following content, replacing card1 with the actual ID you found earlier:
ACTION=="change" \
, KERNEL=="card1" \
, SUBSYSTEM=="drm" \
, ENV{DISPLAY}=":0" \
, ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority" \
, RUN+="/opt/wakeup-display.sh &"
Let’s break down this magic rule:
ACTION=="change"
: This ensures the rule triggers only when the display state changes (i.e., wakes up).KERNEL=="card1"
: This specifies the device ID of your monitor.SUBSYSTEM=="drm"
: This restricts the rule to display devices.ENV{DISPLAY}=":0"
: This checks if the current session is the primary X session.ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority"
: This specifies the path to the X authority file (adjust the path if your login manager differs).RUN+="/opt/wakeup-display.sh &"
: This runs the script located at /home/munim/.local/bin/reset-my-display.sh (which is just a symbolic link to our original script) in the background (&).
Note: Replace /opt/wakeup-display.sh
with the actual path where you want to store the symbolic link to your script.
Testing and Reloading: Putting the Pieces Together Link to heading
Before applying the rule system-wide, let’s test it. Use the following command, replacing /devices/pci0000:00/0000:00:02.0/drm/card1
with the actual path to your display device you noted from the udevadm monitor
output:
udevadm test --action="change" /devices/pci0000:00/0000:00:02.0/drm/card1
This should trigger the script and hopefully display some output indicating success. Finally, reload the udev rules:
sudo udevadm control --reload