Ubuntu – How to run script after resume and after unlocking screen

12.04autostartlock-screensuspendunlock

In Ubuntu 12.04 LTS, I would like to run scripts after resuming from suspend, and after unlocking my desktop. These need to run as my user, and with access to my $DISPLAY.

In particular, I would like to

  • restart nm-applet to work around bug 985028
  • show a custom notification using notify-send
  • possibly other stuff when I get these working

When I resume, scripts in /etc/pm/sleep.d/ are run, but they run as root, without knowledge of my screen and username. It might work if I hard-code my username and export the default DISPLAY :0 in these scripts, but that feels like a very ugly hack.

Scripts in ~/.config/autostart/xyz.desktop run after login, but they don't run after merely unlocking the screen after resume.

Is there a way to run scripts after unlocking the screen after a resume?

Best Answer

It looks like you have to hard code the username in the previous answer anyways, so here's a simple script for in /etc/pm/sleep.d if anyone is looking for a quick fix:

#!/bin/bash 
case "$1" in
    hibernate|suspend)
        sudo -u USERNAME env DISPLAY=:0 zenity --info --text "do stuff on suspend"
        ;;
    thaw|resume)
        sudo -u USERNAME env DISPLAY=:0 zenity --info --text "do stuff on resume"
        ;;
esac