Run Script on Wakeup?

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.


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.


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