How to run script after resume and after unlocking screen

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

This question at the Unix & Linux site documents an alternative approach using dbus messages:

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | ( while true; do read X; if echo $X | grep "boolean true" &> /dev/null; then SCREEN_LOCKED; elif echo $X | grep "boolean false" &> /dev/null; then SCREEN_UNLOCKED; fi done )

(Replace SCREEN_LOCKED and SCREEN_UNLOCKED with the actions you want to perform.)

Using xrandr 1>/dev/null 2>1 as the action on unlocking fixed my problem that monitor resolutions/positions were not being correctly restored on screen unlocking (xrandr seems to cause a re-reading of screen settings). I added this line as a background task in my .bash_profile (strictly it might be better as a desktop file in ~/.config/autostart, since that only runs when you start gnome):

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | ( while true; do read X; if echo $X | grep "boolean false" &> /dev/null; then xrandr 1>/dev/null 2>1; fi done ) &

Further background on the gnome-screensaver API can be found at this site, and on the dbus monitor here.