Run Script on Wakeup? [duplicate]

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.


Solution 1:

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.

Solution 2:

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.

Solution 3:

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

Solution 4:

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...