Search code examples
systemd

Systemd run script after wakeup: "Can't open display"


I have a script which does things with screen brightness, works fine that's cool and now I want to make it run after wake up from suspend.

So I tried using systemd, I have a file under /etc/systemd/system/myscript.service which is as follows:

[Unit]
Description=Run myscript after wakeup
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target


[Service]
Type=oneshot
ExecStart=/usr/local/bin/myscript
User=me
#Environment=DISPLAY=:0

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

Note: User is set because the myscript needs HOME variable.

After I run sudo systemctl enable myscript and try suspend/wakeup, myscript is not run and journalctl -u myscript.service outputs the following message:

Jan 25 13:42:53 mymachine myscript[24489]: Can't open display
Jan 25 13:42:53 mymachine systemd[1]: myscript.service: Succeeded.
Jan 25 13:42:53 mymachine systemd[1]: Finished Run myscript after wakeup.

If I uncomment the line #Environment=DISPLAY=:0 in myscript.service the error is "Can't open display :0"

Any help would be great :^)


Solution

  • This worked on my Arch system. I tested a script in that location with xbacklight going up and down by 75% a few times after a resume from hibernate or suspend (systemctl hibernate / suspend).

    I can only think that you do not have the DISPLAY=:0 in your environment (verify with env) for the user you are running the script as.