Skip to content

Instantly share code, notes, and snippets.

@startergo
Created September 16, 2023 21:17
Show Gist options
  • Save startergo/005ebcb0b1b1de469e2a8b5b68a253ff to your computer and use it in GitHub Desktop.
Save startergo/005ebcb0b1b1de469e2a8b5b68a253ff to your computer and use it in GitHub Desktop.
Prevent idle sleep in a long-running bash script on Mac OS
  • Add this line to the start of the script:
caffeinate -i -w $$ &
  • Caffeinate is a built-in Mac OS utility for creating power management assertions to prevent sleep.
  • The -i flag tells caffeinate to prevent idle sleep.
  • The -w flag tells caffeinate to wait until the specified process finishes.
  • $$ is a shell variable containing the current PID. Together with the -w flag this tells caffeinate to prevent sleep for the duration of the shell script.
  • The final & runs the command in the background so the script can continue running.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment