Syndication Icon
Published October 29, 2019 Updated May 11, 2020

Shell SleepWatcher

  • Duration

    02:35

  • Operating System

    macOS

  • Shell

    bash

  • Terminal

    xterm

Transcript

# Hello and welcome to the Alchemists Screencasts!
# Today, we'll look at using SleepWatcher:
# https://www.bernhard-baehr.de

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

brew install sleepwatcher

# For usage, run:

sleepwatcher

# 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: https://www.alchemists.io/projects/xdg.
# The `support.sh` script is where the bulk of functionality lies:

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

# 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: https://www.alchemists.io/projects/dotfiles.
# The support functions are utilized through the following scripts:

cat "$HOME/.config/sleepwatcher/wake.sh"
cat "$HOME/.config/sleepwatcher/sleep.sh"

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

cat "$HOME/.config/sleepwatcher/plug.sh"
cat "$HOME/.config/sleepwatcher/unplug.sh"

# 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/unplug.sh"

# 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/unplug.sh"

# 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!
# https://www.alchemists.io
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄