58

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.

Eva
  • 893

4 Answers4

64

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.

SpmP
  • 836
  • 7
  • 3
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.

mfisch
  • 3,643
  • 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 06 '12 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 06 '12 at 20:43
  • Realistically what are the implications of "messing up", wouldn't I just need to hard restart and then edit the script appropriately? – Eva Dec 06 '12 at 20:44
  • Yeah, I just added that to my answer. – mfisch Dec 06 '12 at 20:45
  • 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 '14 at 00: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. – Olaf Dietsche Jun 27 '14 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 06 '15 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
sangorys
  • 369
  • 4
  • 12
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...

jamadagni
  • 211
  • 2
  • 12
  • 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 01 '14 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. – David Foerster Mar 22 '15 at 10:20