1

This terminal command sets the computer to sleep mode:

osascript -e 'tell application "Finder" to sleep'

How can I make the computer sleep for a specific amount of time, say 20 minutes? Do I need to issue a separate command to wake the computer up at a specific time?

Thanks

1
  • I updated the script to add sudo -k to revoke elevated privileges from the Terminal session. The fact that the system is going to sleep for a while it probably really isn't necessary as the sudo session will time out in that Terminal before the system awoke if it's sleeps for 10 minutes. However I normally like having it there as it's a good practice in general as once the pmset command are executed there is no need for elevated privileges to remain in that Terminal session. Apr 24, 2016 at 3:49

1 Answer 1

1

Here is a bash script that when run, without an argument, schedules the system to wake in 20 minutes and then puts it to sleep immediately. Or you can supply, as an argument, the number of minutes you want it to sleep before it wakes the system. Unfortunately the OS X command line utility pmset, which does the wake scheduling and sleeping, requires elevated privileges. Meaning the command must be run using sudo as shown in the example output below.

In the example below, the script was saved as sleepwake and then in Terminal made executable using chmod u+x sleepwake, then run just as sleepwake to show the internal help message. (Note: The current working directory I'm in is in my $PATH. See Examples Note:, further below, for additional information.)

$ sleepwake

  This script must be run as root using sudo.

  Syntax:  sudo sleepwake [n]
  Example: sudo sleepwake 10
  Wakes the system 10 minutes after going to sleep.

  Example: sudo sleepwake
  Wakes the system, using the default, 20 minutes after going to sleep.

$  

Creating sleepwake:

In a Terminal, type the following, pressing Enter after each command:

touch sleepwake
open sleepwake

Copy and paste the code below into the opened, blank, document.

Code:

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   printf "\n  This script must be run as root using sudo.\n\n"
   printf "  Syntax:  sudo sleepwake [n]\n  Example: sudo sleepwake 10\n"
   printf "  Wakes the system 10 minutes after going to sleep.\n\n"
   printf "  Example: sudo sleepwake\n"
   printf "  Wakes the system, using the default, 20 minutes after going to sleep.\n\n"
   exit 1
else
            # If no argument (number of minutes) was passed.

    if [[ -z $1 ]]; then
            # Default number of minutes to sleep.
        m="20"
    else
            # Number of minutes to sleep given from command line.
        m="$1"
    fi
        # Do the math and set date time format to
        # what 'pmset schedule wake' requires.

    e="$(date -j -f "%a %b %d %T %Z %Y" "$(date)" "+%s")"
    ((o=60*$m))
    ((e+=o))
    t="$(date -r $e "+%m/%d/%Y %H:%M:%S")"

        # Schedule the 'wake' date time and sleep the system.

    pmset schedule wake "$t"
    pmset sleepnow

        # Revoke elevated privileges from this Terminal session.

    sudo -k

fi

Now save the document, then close it.

In Terminal, make the bash script executable:

chmod u+x sleepwake

Execute the sleepwake command without using sudo, to see that the internal help displays.

Then test it for a minute with the following command:

sudo sleepwake 1

Type in your password when prompted, and press Enter.

The system will now sleep for one minute.


Examples Note: The examples assume that sleep wake is saved in a directory that is in the $PATH, otherwise if not and it's in the current directory then use sudo ./sleepwake, or not in the $PATH or current directory, then use sudo /path/to/sleepwake.


enter image description here

Additional Notes:

The # Do the math... comment in the script means the code block directly below it converts the current date time to seconds since Epoch, in order to add the number of seconds in minutes to sleep to the current date time, and then convert back in order to then have a date time in the format that pmset schedule wake requires and this will be the date time x number of minutes from now, being the date time of execution, incremented either the default 20 minutes or the number of minutes passed as an argument on the command line. Wow! :)

Once the date time to wake has been scheduled and the system put to sleep, it can be woken ahead of schedule if you need to access the system before the scheduled wake date time by the usual means, clicking the mouse or using the keyboard, etc.

For additional information about pmset, see it's man page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .