The settings
On a MacBook, open System Settings › Battery › Options and turn on Prevent automatic sleeping on power adapter when the display is off. On a desktop Mac, the same switch is in System Settings › Energy. How soon the display turns off is set in System Settings › Lock Screen.
The Terminal way: caffeinate
macOS has a command for exactly this. In Terminal:
caffeinatekeeps your Mac awake until you press Control-C.caffeinate -dkeeps the display on as well.caffeinate -t 3600stops after an hour (the number is in seconds).caffeinate -w 1234waits until the process with that number finishes: handy for a long export.caffeinate -sstops the whole system sleeping, but only while it’s plugged in.
Press Control-C or close the Terminal window and your Mac goes back to its usual habits.
With the lid closed
A MacBook keeps working with its lid closed only when it’s plugged in and connected to an external display, keyboard and mouse or trackpad. On a Mac with Apple silicon, approve those accessories first, under System Settings › Privacy & Security › Accessories. Without a display, closing the lid puts it to sleep.
Or one click
SideDock’s Keep Awake switch stops your Mac sleeping until you turn it off, or for a set time, and while it’s on, the notch shows how long is left. It’s also in the Controls tile, next to Dark Mode and Mic Mute.
Try SideDock free for 7 days
Everything unlocked, no card, no account. Pay once if it stays.