- 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
-iflag tells caffeinate to prevent idle sleep. - The
-wflag tells caffeinate to wait until the specified process finishes. $$is a shell variable containing the current PID. Together with the-wflag 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.