Ubuntu – How to get a script to always run on resume in Lubuntu

lubuntuscriptssuspendxfce

I've run into an annoyance on Lubuntu 11.10, described here:

http://ubuntuforums.org/showthread.php?t=1869357

I came up with a workaround, described here:

http://ubuntuforums.org/showpost.php?p=11714061&postcount=30

My workaround involves putting a script in /etc/pm/sleep.d to run on resume.

However, the workaround only seems to work when I suspend using pm-suspend. When I suspend by closing my laptop's lid, my workaround fails. How can I get a script to run when I resume from sleep, and that sleep was triggered by closing the lid?

Details:

I suspect this failure might be because scripts in /etc/pm/sleep.d are not getting executed when I suspend by closing the lid. Suspending with pm-suspend, then resuming by closing/opening the lid works fine. I believe Lubuntu uses xfce4-power-manager to handle suspension due to lid closing, perhaps this is important? I prefer to suspend by closing my laptop's lid, rather than typing a terminal command.

EDIT:

By inspecting /var/log/pm-suspend.log as suggested by andrewsomething, we can see that the reset_panel script runs on every resume, it just fails on resumes that follow lid-triggered suspends. I followed his further suggestion that I add an export statement to my script, and things are working now. The script triggers too early in the wakeup process, but that's not hard to fix. The current version of the script:

#!/bin/bash
case "$1" in
   suspend|hibernate)
      #do nothing
   ;;
   resume|thaw)
      export DISPLAY=:0 #What does this do? Are there side effects?
      sleep 5 && lxpanelctl restart & #Delayed so the battery icon can finish wrecking shop.
   ;;
   *)
      exit 1
   ;;
esac
exit 0

andrewsomething, if you want to post an answer, I'll accept. Thanks for the help!

Best Answer

The fact that the log contained /etc/pm/sleep.d/reset_panel resume suspend: Cant connect to display: indicates that no display is set, and you are trying to run a graphical program. When you're running pm-suspend from your session, the correct display was set probably by LightDM or whatever your display manager is. When it gets run automatically on resume, I assume it's getting run as the root user and not attached to a display. So you need to set the display variable yourself, like so:

#!/bin/bash
case "$1" in
   suspend|hibernate)
      #do nothing
   ;;
   resume|thaw)
      export DISPLAY=:0
      sleep 5 && lxpanelctl restart & #Delayed so the battery icon can finish wrecking shop.
   ;;
   *)
      exit 1
   ;;
esac
exit 0

The only draw back is that this won't work if you display isn't actually :0 That's the default, but it could change particularly in a multi-user setting.

Related Question