Skip to main content

Command Palette

Search for a command to run...

Prompt: Create a simple script to prevent macos from going to sleep

Published
1 min read
A

I’m Aiman, and I see the world through prompts. IBM by day, prompt tinkerer by night. Here I share experiments, failures, wins, and the random magic that happens when curiosity meets AI. Welcome to my lens of ideas {<●>}.

[ Link: https://chatgpt.com/share/6934ada6-1ad0-800f-bf7d-80e24c8adbc4 ]

Here’s a super-simple macOS script that prevents the system from sleeping using the built-in caffeinate command.


✅ Option 1: Run indefinitely (most common)

keep-awake.sh

#!/bin/bash
# Prevent macOS from sleeping indefinitely

caffeinate -dis
  • -d → prevent display sleep

  • -i → prevent system idle sleep

  • -s → prevent system sleep on AC power

➡️ Run it:

chmod +x keep-awake.sh
./keep-awake.sh

Press Ctrl + C to stop and allow normal sleep again.


✅ Option 2: Prevent sleep for a specific duration

Example: keep awake for 2 hours.

#!/bin/bash
# Prevent macOS from sleeping for 2 hours

caffeinate -dis -t $((2 * 60 * 60))

✅ Option 3: Minimal one-liner

If you don’t want a script:

caffeinate -di

This alone prevents macOS from sleeping and turning off display until stopped.


⚡ Bonus: Show a message so you know it’s active

#!/bin/bash
echo "☕ macOS will not sleep while this is running. Press Ctrl+C to stop."
caffeinate -dis

If you want, I can also create a menu bar version, launchd auto-start, or a toggle on/off script.