The caffeinate Terminal Command
Hey guys, this is Matt Cutts on one, and today I'm going to be showing you the caffeinate terminal command. This command is designed to prevent your computer from falling asleep while some important tasks are running that you don't want to get interrupted.
So before I knew about the caffeinate terminal command, I would occasionally find myself going over to System Preferences and finding the Energy Saver pane and setting display sleep all the way up to never. I would do this so that whatever program I was running would continue to run and my computer wouldn't just fall asleep and quit the program or stop the program.
This is obviously a pretty bad solution because, first off, if you forget to reset it, it's not great. Also, because it doesn't exactly fine-tune what's happening to the program that you're running. So maybe I could say, "Okay, the program I'm running is going to take about an hour, so I'll make it so my computer doesn't go to sleep for an hour." But if the program actually takes a little longer than an hour, then your computer would go to sleep and, you know, quit the program. Or if it takes less than an hour, you're, you know, wasting energy.
So what the caffeinate command does is it basically lets you take matters into your own hands. Instead of having to deal with this crude system preference pane, you can prevent your computer from going to sleep in a very specific way.
Now, the most basic usage of caffeinate is caffeinate -i
. When this is running, your computer's display will not fall asleep. This is equivalent to basically going into System Preferences and dragging display sleep all the way up to never. The only difference is it doesn't actually change any settings. When you click the command, it goes back to the way it was before.
So now my computer can fall asleep. This is a really simple way to prevent your computer from going to sleep if you have this running. One thing to note is your computer will still go to sleep if this is running if you close your laptop's lid, or you hold the sleep button, or if your computer runs out of power. But for some people, this is actually enough.
It's basically equivalent to going to System Preferences and dragging the slider, but for a lot of people, this is actually, you know, it's nice, but they can actually get a lot more out of the caffeinate command.
So let's say I have a specific terminal command that I run that I know is gonna take a while, but I don't know exactly how long it's gonna take and I just want to make sure my computer doesn't fall asleep while that task is running. To do this, I can do caffeinate -D
, and then all the arguments after the -D
will be arguments to the command I want to run.
So let's say I want to run an MD5 hash on this file that's right there on my desktop and I don't want my computer to fall asleep while that command is running. I just type the arguments, you know, the first argument is the command name, I hit enter, and you'll notice the caffeinate -D
actually ran this command and when it finished, everything is done.
So wow, while this command was running, my computer actually couldn't fall asleep, or my display wouldn't fall asleep. But now that the command is done, my display would fall asleep. So that's actually already pretty useful.
But sometimes you run a command and you don't realize it's gonna take a while, and then you realize you have to go somewhere and you want to make sure your computer doesn't fall asleep. You can't just quit the command and rerun it with caffeinate because, you know, you would lose some progress or something like that.
So to exemplify this, I'm just going to run a Python command: python console
, and I'm just going to pretend that this is a program that's going to take a long time to run. I can actually attach caffeinate to the process ID of whatever process I want to wait for.
So I can do caffeinate -D -W
, and I'm just going to quickly grab the process ID for this. So right here, it's 2983
, and when I hit enter, caffeinate is going to wait until the process with this process ID is done, and that's our Python process.
So you'll see that caffeinate is running, and when I actually leave Python, caffeinate dies as well. So it was extremely useful for that specific case, and you can really use caffeinate pretty much for any command or task that you are running on your computer. You can use caffeinate to wait until that finishes.
I find that to be pretty much the main use case for this command. There are other things caffeinate can do. For instance, you can set a timeout. So if I use caffeinate -t 2
, for instance, this will wait for two seconds, and for those two seconds, my computer will be unable to fall asleep.
Of course, an equivalent thing would be if I did sleep 2
because it's just running the sleep command. Then there's other things. You can use caffeinate to emulate a mouse movement or a key press, basically to say, you know, to tell your computer that the user is active.
To do that, you can just send a signal basically saying the user is active by doing -u
. I've never actually found this useful, but I guess it would be useful if maybe you were SSH'd into the computer and you wanted to wake it up from display sleep or something like that.
Another thing to note about caffeinate is if you run it with no arguments, it's actually going to prevent system sleep but not display sleep. What that means is if I go back here, you'll notice in System Preferences there were two options I could set: display sleep and computer sleep.
While the display is sleeping, obviously the display turns off, you know, you go to the lock screen if you try to wake up your computer if you have that configured. But programs that are running in the background will still be running until the computer sleeps all together, you know, until actual system sleep.
So if you use caffeinate without a -D
, your display won't fall asleep, but depending on what task you're running in the background, that task will continue to run after the display goes to sleep. So for some who want to save energy, this is actually better than caffeinate -D
.
There are a couple of other things. You know, you can use it to prevent hard drives from spinning down, things like that. But this is basically how I use it. If you want to know more, you can check out the man page by just running man caffeinate
and using the arrow keys, and, you know, use q
to get out.
That will tell you everything you need to know. I really appreciate knowing about this command. My friend actually told me about it, and if I hadn't found out about it, I would have kept doing my same dumb thing of going into System Preferences and changing the setting there.
So thanks for watching "Matt Cutts 101". I hope you learned something. Subscribe and goodbye!