Skip to content

Instantly share code, notes, and snippets.

@tossmilestone
Last active February 9, 2022 08:17
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 tossmilestone/fe06218d34e9e00eda6ff2a14b297084 to your computer and use it in GitHub Desktop.
Save tossmilestone/fe06218d34e9e00eda6ff2a14b297084 to your computer and use it in GitHub Desktop.
Prevent macos to sleep if specified program is running
#!/bin/sh
if [[ -n "$1" ]]; then
pid=$(ps rax | awk '{print $1, $5}' | grep -i "$1" | cut -d ' ' -f 1)
if [[ -n $pid ]]; then
caffeinate -s -w "$pid" &
echo "Systemp sleep prevented by $1"
else
echo "Sorry, the $1 could not be found."
fi
else
echo "Please enter the name of the program that you want to wait."
echo "Example:"
echo " prevent_sleep wget"
fi
@tossmilestone
Copy link
Author

Run pmset -g to verify if the script is working:

❯ pmset -g
System-wide power settings:
Currently in use:
 lidwake              1
 lowpowermode         0
 standbydelayhigh     86400
 standbydelaylow      0
 standby              1
 proximitywake        1
 ttyskeepawake        1
 hibernatemode        3
 powernap             0
 gpuswitch            2
 hibernatefile        /var/vm/sleepimage
 highstandbythreshold 50
 displaysleep         10
 womp                 1
 networkoversleep     0
 sleep                1 (sleep prevented by caffeinate)
 acwake               0
 halfdim              1
 tcpkeepalive         1
 disksleep            10

the line sleep 1 (sleep prevented by caffeinate) should have caffeinate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment