The letter A styled as Alchemists logo. lchemists Syndication Icon

Putin's War on Ukraine - Watch President Zelenskyy's speech and help Ukraine fight against the senseless cruelty of a dictator!

Published October 29, 2019 Updated May 11, 2020

Shell SleepWatcher

Demonstrates using the SleepWatcher program for automating machine behavior when connected to power, sleeping, etc.


# Hello and welcome to the Alchemists Screencasts!
# Today, we'll look at using SleepWatcher:

# SleepWatcher provides powerful macOS state change automation.
# To install, run:

brew install sleepwatcher

# For usage, run:


# Much can be automated with SleepWatcher but we'll focus on the following:
# `--wakeup` - Executes when machine wakes from sleep, opening laptop, etc.
# `--sleep` - Executes when machine goes to sleep, closing laptop, etc.
# `--plug` - Executes when machine is connected to power.
# `--unplug` - Executes when machine is disconnected from power.

# For my setup, I use an XDG configuration:

ls -A1 "$HOME/.config/sleepwatcher"

# 💡 To learn more about XDG, visit:
# The `` script is where the bulk of functionality lies:

cat "$HOME/.config/sleepwatcher/"

# The above provides common functionality in the form of:
# - Machine decaffeination.
# - Machine mute/unmute.
# - Machine WiFI on/off.
# - Machine Bluetooth on/off.
# ...etc...

# 💡 For source code, visit:
# The support functions are utilized through the following scripts:

cat "$HOME/.config/sleepwatcher/"
cat "$HOME/.config/sleepwatcher/"

# As you can see, these scripts are relatively simple.
# Same goes for the power scripts:

cat "$HOME/.config/sleepwatcher/"
cat "$HOME/.config/sleepwatcher/"

# All of the above can be configured via a single configuration file:

cat "$HOME/.config/sleepwatcher/configuration"

# To break this down further, the above says:
# - Run SleepWatcher as a background daemon.
# - Be verbose with the output.
# - Use custom sleep, wakeup, plug, and unplug scripts.
# 💡 Each configuration option *must* be on a separate line.

# SleepWatcher can be run as follows:

sleepwatcher --verbose --unplug "$HOME/.config/sleepwatcher/"

# Only problem is this ends up being a foreground task.
# We need SleepWatcher to run in the background in order to be effective.
# Thankfully, SleepWatcher can be daemonized:

sleepwatcher --daemon --verbose --unplug "$HOME/.config/sleepwatcher/"

# To confirm, we can run:

ps axu | rg --invert-match rg | rg "sleepwatcher" --ignore-case
pkill -15 sleepwatcher

# I killed the SleepWatcher process because there is a better way to run it:

sleepwatcher --config "$HOME/.config/sleepwatcher/configuration"
ps axu | rg --invert-match rg | rg "sleepwatcher" --ignore-case

# The above ensures the full configuration, shown earlier, is used.
# Being able to daemonize SleepWatcher means you can script it to run at boot.
# There is much you can do with SleepWatcher, so please explore further.
# Hopefully, this gives you a few examples of how to automate your machine.

# Enjoy!
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄