Skip to content

Instantly share code, notes, and snippets.

@yi-jiayu
Created December 25, 2018 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yi-jiayu/f1a8f32d70a6a4c3b7dbd81589c40f0d to your computer and use it in GitHub Desktop.
Save yi-jiayu/f1a8f32d70a6a4c3b7dbd81589c40f0d to your computer and use it in GitHub Desktop.
Shell script to display a notification if there are any processes preventing the system from sleeping on macOS (https://blog.jiayu.co/2018/12/get-notified-when-a-process-is-preventing-sleep-on-macos/)
#!/bin/sh -
sleep_blocker=$(pmset -g | grep -m1 "sleep prevented by" | sed -E 's/.+sleep prevented by (.+)\)$/\1/')
if [ ! -z "$sleep_blocker" ]; then
osascript -e "display notification \"$sleep_blocker\" with title \"Sleep prevention warning\" subtitle \"The following processes are preventing sleep:\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment