Kill Programs On Sleep Improved

Kill Programs On Sleep Improved

This is an expansion on a post I originally made on MyAppleAnd.Me, smartly titled Kill Programs on Sleep. Since I wrote that post, I’ve made changes to how SleepWatcher executes.

From SleepWatcher’s website:

SleepWatcher 2.2 (running with Mac OS X 10.5 to 10.7, source code included) is a command line tool (daemon) for Mac OS X that monitors sleep, wakeup and idleness of a Mac. It can be used to execute a Unix command when the Mac or the display of the Mac goes to sleep mode or wakes up, after a given time without user interaction or when the user resumes activity after a break or when the power supply of a Mac notebook is attached or detached. It also can send the Mac to sleep mode or retrieve the time since last user activity.

Basically, SleepWatcher runs when your laptop goes to sleep or wakes up. It can do more, but these are two areas I care about. SleepWatcher works via launchd, which is awesome, but also makes modification somewhat confusing. Since it will execute a command, you can have it execute a bash script or Perl script or whatever. However, if you have 15 commands you want to execute, the script can get a little long. And if you change the script, you have re-execute launchctl. Or …

You could have the script execute every command that it finds in a directory. And all you’d have to do is add/remove commands from the directory in order to change how SleepWatcher operates. That’s what we’re going to do here.

First, create the following directories:

mkdir ~/sleep-commands mkdir ~/wake-commands

Now create the following script in ~/Applications.

!/bin/sh

FILES=~/sleep-commands/*

for f in $FILES do /usr/bin/open $f done

Save it as Sleep-Commands.sh and make it executable:

chmod +x Sleep-Commands.sh

This script executes every file in ~/sleep-commands. These will be the commands we want to execute when the system goes to sleep. Now let’s repeat the process for commands to execute when the system wakes. Call this one Wake-Commands.sh:

!/bin/sh

FILES=~/wake-commands/*

for f in $FILES do /usr/bin/open $f done

Now make it executable:

chmod +x Wake-Commands.sh

Okay, so now we have sleeping and waking covered. Let’s move to our SleepWatcher config file. SleepWatcher comes with two sample configuration files, depending on whether you want to execute system-wide or only as you. We’re going to do this as you, the logged-in user. If you want it system-wide, see SleepWatcher’s ReadMe file. It’s very straightforward.

The default plist we’re working from has a very long name, so let’s change that:

mv de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist de.bernhard-baehr.sleepwatcher.plist

Now edit the file edit the file and change these lines:

-s ~/.sleep -w ~/.wakeup

To this:

-s /Users/stephen/Applications/Sleep-Commands.sh -w /Users/stephen/Applications/Wake-Commands.sh

Obviously, change /Users/stephen as applicable. Save the file, move it, and load it into launchd:

mv de.bernhard-baehr.sleepwatcher.plist ~/Library/LaunchAgents

# launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist

At this point, we’ve got SleepWatcher configured to execute sleep commands (everything in ~/sleep-commands) and wake commands (everything in ~/wake-commands). Now all we have to do is use Automator to create the commands we want executed. Put the sleep commands in ~/sleep-commands and the wake commands in ~/wake-commands. And you’re done. Now you can add and remove commands as necessary from each directory without bothering with reloading/relaunching the main SleepWatcher script from launchd.

Note: Due to the open command, this won’t execute non-Automator programs as expected. However, it’s easy enough to add if statements to deal with it. As always, your mileage may vary and probably will. If you have issues or problems, let me know.

    Related Posts