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
Launch Power Manager.app
Click Add… to create a new event
Select the Run a script on-demand task
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"
Continue passed the Constraints step
Continue to the Why step and describe your event
Continue and Add the event.
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.
Open the event in the event editor; do this by holding down the Option or Alt key and double-clicking on the event.
Select Add an action and select Await Log Out.
Again, select Add an action and this time select Run script.
Edit the Run script action; do this by selecting Edit from the action’s Cog pop-menu.
Copy and paste in the second script to perform. This script will run only after the user has logged out.
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.
- Apply the action changes and Save the changes to the event.
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.