Automatically powering off monitor on suspend

· by Raghu Rajagopalan · Read in about 1 min · (153 words) ·

So ddccontrol does the job of switching monitors on and off…​ It’s nice to have it run on suspend/resume - esp late night when I switch off, then go to bed and see the monitor’s orange light driving me nuts 🙁

monitor-suspend.service
[Unit]
Description=ddccontrol system suspend actions
Before=nvidia-suspend.service

[Service]
Type=oneshot
ExecStart=/usr/bin/logger -t suspend -s "monitor-suspend.service"
ExecStart=/usr/local/bin/monitor-ddc.sh "pre"

[Install]
RequiredBy=systemd-suspend.service
monitor-resume.service
[Unit]
Description=NVIDIA system resume actions
After=systemd-suspend.service
After=systemd-hibernate.service
After=nvidia-resume.service

[Service]
Type=oneshot
ExecStart=/usr/bin/logger -t suspend -s "monitor-resume.service"
ExecStart=/usr/local/bin/monitor-ddc.sh  "post"

[Install]
RequiredBy=systemd-suspend.service
RequiredBy=systemd-hibernate.service
monitor-ddc.sh
#! /bin/bash
# symlink this to /usr/local/bin

i2cdevices=$(ddccontrol -p | grep 'Device: ' | sed -e 's/.*: //')
readarray -t <<<"$i2cdevices"
for i2cdev in "${MAPFILE[@]}"
do
    echo "$i2cdev"

    case "$1" in
        "pre")
            ddccontrol  -r 0xd6 -w 5 "$i2cdev"
            ;;
        "post")
            ddccontrol  -r 0xd6 -w 1 "$i2cdev"
            ;;
        *)
            exit 1
    esac
done

link the files above to /etc/systemd/system and then enable both of them with systemctl enable monitored-suspend and systemctl enable monitor-resume