Last active
          December 27, 2022 15:31 
        
      - 
      
- 
        Save atreeon/c2d280576b2e95877d1d123efacd8066 to your computer and use it in GitHub Desktop. 
    sleep mac
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function slx { | |
| echo "...stopping any backups, closing any programmes known to cause issues" | |
| tmutil stopbackup | |
| pkill -x qemu-system-i386 | |
| pkill -x qemu-system-x86_64 | |
| pkill -x Music | |
| pkill -x VLC | |
| echo "...turning off bluetooth & ejecting external drive" | |
| blueutil --power 0 | |
| # diskutil eject /Volumes/SanDisk512TimeMachine | |
| retry_count=0 | |
| max_retries=4 | |
| while true; do | |
| canSleep=$(isPreventingSleep) | |
| if [ $canSleep -eq 1 ]; then | |
| echo "success" | |
| break | |
| else | |
| retry_count=$(($retry_count+1)) | |
| if [ $retry_count -eq $max_retries ]; then | |
| pmset -g assertions | |
| blueutil --power 1 | |
| say "cannot sleep" | |
| echo "cannot sleep" | |
| break | |
| fi | |
| echo "...trying again" | |
| sleep 4 | |
| fi | |
| done | |
| } | |
| extractpmset() { | |
| pmset -g assertions | awk 'NR>=3&&NR<=10' | grep -v "UserIsActive" | grep -v "ExternalMedia" | |
| #pmset -g assertions | awk 'NR>=3&&NR<=10&&!/UserIsActive/' | |
| } | |
| isPreventingSleep() { | |
| output=$(extractpmset) | |
| if [[ $output == *"1"* ]]; then | |
| echo "1" | |
| else | |
| echo "0" | |
| fi | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment