57

Is there a way to run a script when a computer "wakes up" (from sleeping)? I know it can be done on startup, obviously, but the issue in this case is that the backlight of my laptop seems to reset every time it wakes up from sleep, and I'd ideally like to have a script take care of turning it down to a reasonable level instead of having to type it in manually every time.

0

4 Answers 4

63

In 15.04, Vivid, you need to place your scripts in:

/lib/systemd/system-sleep/

An example script based on one from the Arch wiki (systemd sleep Hooks):

#!/bin/sh
case $1/$2 in
  pre/*)
    echo "Going to $2..."
    # Place your pre suspend commands here, or `exit 0` if no pre suspend action required
    ;;
  post/*)
    echo "Waking up from $2..."
    # Place your post suspend (resume) commands here, or `exit 0` if no post suspend action required
    ;;
esac

Dont forget to make your script executable!

sudo chmod a+x /lib/systemd/system-sleep/your-script

See man systemd-sleep for more details.

There is no need for sudo as your script will be run as root.

note: the Arch wiki link on the subject (systemd sleep hooks) states (incorrectly for 15.04) that you should place your scripts in /usr/lib/systemd/system-sleep/, but this is will not work in Ubuntu 15.04. Place your scripts in /lib/systemd/system-sleep/ if you're running 15.04.

6
  • Shouldn't those scripts rather go in /etc somewhere like /etc/pm/sleep.d? May 16, 2015 at 16:29
  • That is my thought too, but this is where they go.
    – SpmP
    May 21, 2015 at 19:40
  • 4
    Thanks for that - spent ages trying to work out why my scripts in /etc/pm/sleep.d aren't being run on 15.10. Dec 20, 2015 at 11:25
  • @NickCraig-Wood: I envy you - I'd like to survive ages ... Aug 20, 2021 at 10:03
  • 1
    Works in 22.04. Jan 17, 2023 at 16:25
39

pm-utils provides a bunch of scripts that run on sleep/resume, you could add your script there, but you'll need to be careful as screwing up will likely break resume. Look in /usr/lib/pm-utils/sleep.d, that's where the scripts are, you can look at the script called 95led as it's quite simple and will be a good model to start with.

95led provides cases for hibernate/suspend and thaw/resume, if you only want resume, you'd write your script like this:

#!/bin/sh

case "$1" in
    resume)
        echo "hey I just got resumed!"
        run_some_command
esac

Your script should probably run last, so make sure it shows up last in the directory, maybe name it 99ZZZ_myscript or something. Again, if you're not sure what you're doing here, I wouldn't mess with it. You may end up breaking suspend/resume. If that happens you can delete the script or fix it, but you'll have to do a hard power-cycle to get your system back up.

There may also be a simpler way, but I know this method will work.

7
  • thanks for the suggestion.. I'll wait and see if there's a safer way (if nobody comes up with anything I'll give it a shot / accept your answer)
    – Eva
    Dec 6, 2012 at 20:41
  • If you test your script well before sticking it into being called by the resume script I think you should be okay, just test it independently well. Some people aren't even comfortable using sudo and I want to make sure I don't lead someone down the wrong path. I think worst case here is that your laptop fails to resume and you'd have to power cycle it and fix your script.
    – mfisch
    Dec 6, 2012 at 20:43
  • 1
    I can't get this to work. My script works fine (does what it should when I run in from the terminal), and is named 99ZZZ_scriptsOnWake in /usr/lib/pm-utils/sleep.d/ and it is executable but nothing happens on wake. What could I be missing?
    – lindhe
    Jan 15, 2014 at 0:06
  • 1
    @Lindhea Typical problems are environment variables, which are present in your terminal, but aren't when run on suspend and resume. You can put debug output in your script, to see, if it runs at all. Jun 27, 2014 at 19:31
  • 4
    This does not work with systemd as in 15.04 onwards, see my answer below. Very similar to the above, simple as 8)
    – SpmP
    May 6, 2015 at 23:01
14

As I dont have enough reputation to write comments, I will rewrite the solution from mfisch in adding the answer for Lindh-E :

SOLUTION :

pm-utils provides a bunch of scripts that run on sleep/resume.

What you need is to :

  1. Create a script called 99MyScript.sh (99 allows to run it after other scripts)
  2. Add your script in /usr/lib/pm-utils/sleep.d
  3. Assign execution right : sudo chmod +x /usr/lib/pm-utils/sleep.d/99MyScript.sh
  4. Test it :)

WARNING :

you'll need to be careful as screwing up will likely break resume. If the system crashes or freezes, you can delete the script or fix it, but you'll have to do a hard power-cycle to get your system back up.

EXAMPLE :

Look the existing scripti in /usr/lib/pm-utils/sleep.d. The script called 95led is quite simple and will be a good model to start with. 95led provides cases for hibernate/suspend and thaw/resume, if you only want resume, you'd write your script like this:

#!/bin/sh

case "$1" in
    resume)
        echo "hey I just got resumed!"
        run_some_command
esac
7

This is not really a separate independent answer but since I'm unable to comment (due to reputation restriction) I'd just like to add an important supplement to mfisch's answer which also worked for me.

Please note that the HOME and PATH (and quite possibly other environmental variables) will probably not be the same as your regular shell environment. I had problems with mfisch's solution not seeming to work for the scripts I needed to execute on resume but it turned out that it was my scripts at fault since they were expecting HOME to be set to my user directory (I'm a single user on my system) and for PATH to include /usr/local/bin, and at the point of resume, neither of this is guaranteed (and in the case of HOME, possibly inadvisable). So you may have to adapt your scripts/programs to not depend on those env-vars...

2
  • 1
    yeah, reputation system sometimes sucks. Not once I didn't contribute because of low reputation (newb on that site) to post a comment. Anyway good point +1 for you.
    – dmnc
    Dec 1, 2014 at 13:51
  • 2
    If your feel that your contribution is a worthwhile improvement on another answer, please suggest an amendment to it with the edit link below it. Mar 22, 2015 at 10:20

Not the answer you're looking for? Browse other questions tagged .