Great title, I know.

If there’s something that needs to be done whenever you wake your computer from suspend, hibernate, or just before it suspends/hibernates, you can do it without too much difficulty if you know how to script.

The Madwifi driver for my Thinkpad’s wireless card doesn’t like to be suspended and I always had to reset it every time I awoke my computer. Annoying! So from several sources I have found that putting a script in /usr/lib/pm-utils/sleep.d/ would let me do this. However, they have to adhere to certain guidelines. First of all, you define what the script handles for each change (sleep/wake) in a single script. A bare Bash script that does this is as follows:


#!/bin/bash
case $1 in
    hibernate)
        ;;
    suspend)
        ;;
    thaw)
        ;;
    resume)
        ;;
    *)  echo "ERROR: called incorrectly."
        ;;
esac

This is pretty simple. Now just call whatever you need in the corresponding parts (thaw means wake from hibernation).
My wireless script (I didn’t bother with hibernation because I never use it) looks like this:


#!/bin/bash
case $1 in
    hibernate)
        ;;
    suspend)
	if [ -n "$(lsmod | grep ath_pci)" ]; then modprobe -r ath_pci; fi
        ;;
    thaw)
        ;;
    resume)
	if [ -z "$(lsmod | grep ath5k)" ]; then modprobe ath_pci; fi
        ;;
    *)  echo "ERROR: used incorrectly."
        ;;
esac

It checks to see if I’m using ath5k (which I do sometimes), and if I’m not loads/unloads ath_pci as necessary. Works like a charm.
Now there are two more steps. You have to save the file as ##something to tell pm-suspend when to call the script. The higher the number, the earlier the script will be called, so if your script depends on things being set up, it generally needs to be fairly low. I wanted mine to run before 55NetworkManager but after 75modules, so I named the file 57athpci.

Finally, set the file as executable by running chmod +x filename (so in my case chmod +x 57athpci). You’re all set.

I know this works in Fedora, and also ArchLinux (my tutorial is modified and personalized from their tutorial); any feedback on other distributions would be nice. (Especially Ubuntu).

Advertisement