How to Wait for Log Out

How to make your scripts wait for the user to log out before continuing. In this recipe we use Power Manager's await log out action.

A performing Power Manager event can wait until the user logs out. In this recipe we walk through how to add the await log out action to your Power Manager events.

Many scripted tasks require waiting for the user to log out. Tasks such as completing larger software updates can only safely be completed once the computer is free of active users.

Let’s create an event that can be initiated by a user but only completes once the user has logged out.

In this recipe the first part of the event will show a message to the active user. The event will then pause and wait for the user to log out. Only then will the second part of the event be performed.

You could use the first part to begin a file download or start preparing the computer for an update. In the second part the installation could take place, knowing the user is not logged in.

Create an Await Log Out Event

  1. Launch Power Manager.app

  2. Click Add… to create a new event

    Click Add… to begin creating the event

  3. Select the Run a script on-demand task

    Select the Run a script on-demand task

  4. In Script step, copy and paste in the first half of the script:

    #!/usr/bin/osascript
    
    display dialog "Please log out soon." buttons "OK" default button "OK"
    

Copy and paste in your AppleScript

  1. Continue passed the Constraints step

    Continue passed the Constraints for this recipe

  2. Continue to the Why step and describe your event

    Name and describe your new event

  3. Continue and Add the event.

    Confirm the event can be created

The first half of the event is ready. If you trigger the event now, an AppleScript dialog will be displayed. However, the event ends once the dialog is displayed.

Next, we will extend the event with two more actions. The first action will perform the wait until log out occurs and the second will speak a short message to demonstrate the event observed the user logging out.

  1. Open the event in the event editor; do this by holding down the Option or Alt key and double-clicking on the event.

    Open the event in the event editor

    Add an action from below the last action

  2. Select Add an action and select Await Log Out.

    Select the Await Log Out action

    Add a Run script action

  3. Again, select Add an action and this time select Run script.

Add a Run script action

  1. Edit the Run script action; do this by selecting Edit from the action’s Cog pop-menu.

  2. Copy and paste in the second script to perform. This script will run only after the user has logged out.

    #!/bin/sh

    say “Thank you for logging out.”

Be sure to set the Short User Name to root or another non-active user. When this script is performing there will not be an active or logged in user available.

Copy and paste in the second script to run as root

  1. Apply the action changes and Save the changes to the event.

Your log out waiting event is ready

The event is ready to test. When triggered the event will immediately perform the first script action.

The first script runs as the active user and shows an AppleScript based alert. The alert asks the user to log out but does nothing more. The user is free to continue working for as long as required.

At this point the await log out action begins. This action will pause the event and only resume once a log out occurs.

Once log out begins, the event will continue and the second run script action will be performed. This time the script will be run as the root user and, in our example, will speak a few words.

This technique of performing, pausing, and resuming allows for complex sequences of events. You should prefer this approach over loops and delays in your own scripts. Power Manager is extremely efficient and does not require processor time while waiting – unlike most scripting approaches.