How to Stop Your Mac Sleeping While Time Machine is Running

You can tell Power Manager not to put your Mac to sleep while Time Machine is running. In this recipe, we walk through how to add this condition to your sleep event.

Time Machine is macOS's included back-up solution. Time Machine works in the background and quietly maintains a copy of your files and folders on an external storage device. Commonly the external device is a small inexpensive hard drive plugged into your computer by a USB cable.

If you do not have Time Machine set up on your Mac, I highly recommend it. The back-up is inexpensive, private, and, once set-up, need not be thought about again.

Time Machine is designed to work on a Mac computer that is being powered on and off, going to sleep and waking. Interruptions and changes to the Mac's environment are expected and Time Machine will pause and resume its tasks as needed.

Depending on the version, macOS will postpone idle triggered sleep when Time Machine is running.

By default, Power Manager will not postpone inactivity triggered sleep when Time Machine is running. However, there are environments where giving Time Machine priority over sleep is desirable. Let's look at how to give Time Machine priority over inactivity triggered sleep.

Event Conditions

Power Manager supports conditions. Conditions are parts of an event that are evaluated when the event it triggered but before the event's actions are performed.

Only if an event's conditions met, will the event's actions be performed.

For this recipe, we need to add a condition that only passes if Time Machine is not running.

Inverting Conditions

As an aside, individual conditions can be inverted within Power Manager. Thus you could add a condition to check if Time Machine is running, then invert the result for the evaluation. Thus if you find it easier to check for the postive nature of a condition but want the negative outcome, do this by inverting the condition:

  • Condition > Edit (cog menu) > Optional (pop-up menu) > Invert

Invert is an Optional for conditions in Power Manager

Is Time Machine Running?

So how do you know if Time Machine is running on macOS? As a user, the Time Machine menu bar icon is the obvious location to check. As a process running on macOS, visually checking a menu is not possible.

Thankfully macOS includes a command line tool called tmutil; tmutil stands for Time Machine Utility. We can use this tool to determine when Time Machine is running.

The tmutil tool is not ideal because the output format is undocumented and may change between versions of macOS. For our immediate needs, tmutil provides enough information to work with but I would avoid relying too heavily on the format remaining unchanged.

Try running the command tmutil status and looking at the result:

  1. Launch Applications > Utilities > Terminal
  2. Enter the command: tmutil status

Depending on Time Machine's status at the time, the result will look something like:

$ tmutil status
Backup session status:
{
  BackupPhase = Copying;
  ClientID = "com.apple.backupd";
  DateOfStateChange = "2018-05-16 08:54:13 +0000";
  DestinationID = "00000000-0000-FFFF-FFFF-00FF00FF00FF";
  DestinationMountPoint = "/Volumes/Time Machine Backups";
  Percent = "0.4045578256793293";
  Progress =     {
    TimeRemaining = 1240;
    "_raw_totalBytes" = 380353906;
    bytes = 170972388;
    files = 14136;
    totalBytes = 418389296;
    totalFiles = 14136;
  };
  Running = 1;
  Stopping = 0;
  "_raw_Percent" = "0.4495086951992548";
}

Note the line near the base of the output that reads: Running = 1;. This is the line we care about.

When Time Machine is running, the line reads:

  Running = 1;

When Time Machine is idle, the line reads:

  Running = 0;

We can use another tool, called grep, to determine if which running line appears in the output. Our Power Manager condition will use a combination of the tmutil and grep tools to know when Time Machine is running.

The condition's script will be:

#!/bin/sh
tmutil status | grep 'Running = 0;'

Missed Opportunity

For the technically inclined, the tmutil output is almost in JSON format. Had Apple left out the first line Backup session status:, then we could have programmatically parsed the rest and written a more robust script. Pattern matching with grep is less robust than parsing and working with a data structure.

Creating a Conditional Sleep Event

Let's add the Time Machine script as a condition to an existing inactivity sleep event. If you do not have an event to customise, see How to Make Your Mac Sleep After Inactivity to create one.

  1. Launch Power Manager.app Launch Power Manager on macOS
  2. Open your existing sleep after inactivity event in the Event Editor; select the event, then hold the Option key and select the menu item Edit > Edit Event Editor… Power Manager's event editor
  3. Add a Run Script > Shell Script condition from the pop-up menu Add a condition Three part Power Manager event: trigger, condition, and action
  4. Edit the condition; select Edit from the condition's cog menu in the top-right of the condition box. Edit the event's condition
  5. Copy and paste in the condition script into the Script Field:
     #!/bin/sh
     tmutil status | grep 'Running = 0;'
     
  6. Set the Short User Name to root; for this recipe you can use any non-graphical user. Paste in the script and set the user name
  7. Apply and Save the changes; select Apply in the condition and Save for the event. Save the changes to complete the event

Your conditional event is complete and ready to test.

You should now test your new event. You can do this by starting a Time Machine back-up, and then triggering your new Power Manager event. Trigger the event using either the menu item in the top-right of your menu bar, or through the Power Manager.app > Scheduler (menu) > Show Engine interface.

When Time Machine is running, the event will no longer put your Mac to sleep.

This article was posted in , , and and tagged , , , and .

Published by Graham Miln on