{"id":4786,"date":"2026-02-03T17:01:59","date_gmt":"2026-02-03T17:01:59","guid":{"rendered":"https:\/\/nikola-breznjak.com\/blog\/?p=4786"},"modified":"2026-02-03T17:01:59","modified_gmt":"2026-02-03T17:01:59","slug":"caffeinate-your-mac-to-prevent-it-from-sleeping","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/quick-tips\/caffeinate-your-mac-to-prevent-it-from-sleeping\/","title":{"rendered":"Caffeinate your Mac to prevent it from sleeping"},"content":{"rendered":"<h2>TL;DR<\/h2>\n<p>I&#8217;ll show you how to use macOS&#8217;s built-in <code>caffeinate<\/code> command to keep your Mac awake:<\/p>\n<ul>\n<li><strong>until you stop it<\/strong><\/li>\n<li><strong>for a set amount of time<\/strong>, or<\/li>\n<li><strong>only while a long-running command is executing<\/strong><\/li>\n<\/ul>\n<p>No additional apps that you need to install, no menu bar junk, no &quot;why is my Mac sleeping again?!&quot; drama.<\/p>\n<h2>Why you&#8217;d care about <code>caffeinate<\/code><\/h2>\n<p>If you&#8217;ve ever kicked off a long download, <code>rsync<\/code> backup, Docker build, video export, or &quot;I&#8217;ll just run this migration quickly&quot; job\u2026 and then came back to find your Mac politely sleeping like an innocent kitten \u2014 you already know why this matters.<\/p>\n<p>macOS has an official tool for this, and it&#8217;s been sitting right under your nose:<\/p>\n<pre><code class=\"language-bash\">caffeinate<\/code><\/pre>\n<p>It&#8217;s basically the Terminal equivalent of &quot;No, macOS, we are <em>not<\/em> done here.&quot;<\/p>\n<h2>Option #0: The simplest possible usage<\/h2>\n<p>Open Terminal and run:<\/p>\n<pre><code class=\"language-bash\">caffeinate<\/code><\/pre>\n<p>That&#8217;s it. Your Mac won&#8217;t go idle-sleep as long as this process is running.<\/p>\n<p>To stop it press <strong>Ctrl + C<\/strong><\/p>\n<h2>Option #1: Keep the Mac awake for a fixed amount of time<\/h2>\n<p>Sometimes you just want to keep your Mac awake for 2 hours (and keep that Slack icon \ud83d\udfe2 <em>hey, I don&#8217;t judge<\/em>), and then you&#8217;d pass in the <code>-t<\/code> parameter (which indicates seconds):<\/p>\n<pre><code class=\"language-bash\">caffeinate -t 7200<\/code><\/pre>\n<p>That&#8217;s 2 hours (60 <em> 60 <\/em> 2 = 7200). <em>Yes, we still count seconds like it&#8217;s 1999.<\/em><\/p>\n<p>A few common ones:<\/p>\n<ul>\n<li>30 minutes: <code>1800<\/code><\/li>\n<li>1 hour: <code>3600<\/code><\/li>\n<li>2 hours: <code>7200<\/code><\/li>\n<li>4 hours: <code>14400<\/code><\/li>\n<\/ul>\n<h2>Option #2: Keep the display awake as well<\/h2>\n<p>If for some reason you want to prevent your display from dimming, use the <code>-d<\/code> switch:<\/p>\n<pre><code class=\"language-bash\">caffeinate -d<\/code><\/pre>\n<h2>Option #3: The best trick \u2014 keep Mac awake <strong>only while a command runs<\/strong><\/h2>\n<p>This is the <em>chef&#8217;s kiss<\/em> use case; instead of running <code>caffeinate<\/code> separately, wrap your long-running command with it:<\/p>\n<pre><code class=\"language-bash\">caffeinate -i your_command_here<\/code><\/pre>\n<p>Example: big rsync backup:<\/p>\n<pre><code class=\"language-bash\">caffeinate -i rsync -av ~\/Pictures\/ \/Volumes\/Backup\/Pictures\/<\/code><\/pre>\n<p>Example: long npm build:<\/p>\n<pre><code class=\"language-bash\">caffeinate -i npm run build<\/code><\/pre>\n<p>Example: running tests that take forever because you&#8217;re &quot;testing thoroughly&quot; (<em>and definitely not because of that one integration suite<\/em>):<\/p>\n<pre><code class=\"language-bash\">caffeinate -i .\/run-tests.sh<\/code><\/pre>\n<p>When your command finishes, <code>caffeinate<\/code> stops automatically. No cleanup required. No &quot;wait, did I leave it running overnight?&quot; paranoia.<\/p>\n<h2>Option #4: Run <code>caffeinate<\/code> in the background<\/h2>\n<p>If you want to keep your Terminal usable:<\/p>\n<pre><code class=\"language-bash\">caffeinate -d -i &amp;<\/code><\/pre>\n<p>That <code>&amp;<\/code> sends it to the background.<\/p>\n<p>Want to see it?<\/p>\n<pre><code class=\"language-bash\">ps aux | grep caffeinate<\/code><\/pre>\n<p>Want to stop it?<\/p>\n<p>Option A: bring it back to foreground and stop:<\/p>\n<pre><code class=\"language-bash\">fg<\/code><\/pre>\n<p>then hit <strong>Ctrl + C<\/strong><\/p>\n<p>Option B: kill it by PID (you&#8217;ll see PID in the <code>ps<\/code> output):<\/p>\n<pre><code class=\"language-bash\">kill &lt;PID&gt;<\/code><\/pre>\n<h2>Handy flag cheat sheet<\/h2>\n<p>Here&#8217;s the &quot;print this on a sticky note&quot; version:<\/p>\n<ul>\n<li>\n<p>Keep system awake until stopped:<\/p>\n<pre><code class=\"language-bash\">caffeinate<\/code><\/pre>\n<\/li>\n<li>\n<p>Keep awake for 1 hour:<\/p>\n<pre><code class=\"language-bash\">caffeinate -t 3600<\/code><\/pre>\n<\/li>\n<li>\n<p>Keep display awake:<\/p>\n<pre><code class=\"language-bash\">caffeinate -d<\/code><\/pre>\n<\/li>\n<li>\n<p>Keep awake while a command runs:<\/p>\n<pre><code class=\"language-bash\">caffeinate -i <command><\/code><\/pre>\n<\/li>\n<li>\n<p>Display + system, background:<\/p>\n<pre><code class=\"language-bash\">caffeinate -d -i &<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>Bonus: a tiny alias (because typing is hard)<\/h2>\n<p>If you find yourself using it often, add this to your <code>~\/.zshrc<\/code> (or <code>~\/.bashrc<\/code>):<\/p>\n<pre><code class=\"language-bash\">alias awake=&#039;caffeinate -d -i&#039;<\/code><\/pre>\n<p>Reload:<\/p>\n<pre><code class=\"language-bash\">source ~\/.zshrc<\/code><\/pre>\n<p>Now you can do:<\/p>\n<pre><code class=\"language-bash\">awake rsync -av ~\/Stuff\/ \/Volumes\/Backup\/Stuff\/<\/code><\/pre>\n<p>And voil\u00e0 \u2014 your Mac stays awake, your command runs, and you look like you totally planned it this way.<\/p>\n<p>Hope this was useful, and see you next time \ud83d\udcaa<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR I&#8217;ll show you how to use macOS&#8217;s built-in caffeinate command to keep your Mac awake: until you stop it for a set amount of time, or only&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4787,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-4786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-quick-tips"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=4786"}],"version-history":[{"count":0,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4786\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/4787"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=4786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=4786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=4786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}