How to Run a Script Before an Inactivity Triggered Log Out

Power Manager can automatically log out an inactive Mac. The approach is predictable and unaffected by applications attempting to block or interrupt the log out with last minute dialogs.

We have previously walked through how to create an inactivity triggered log out event. We have also looked at how to add a warning prior to the log out.

In this recipe we are going to extend the event further. We are going to run a script just before the log out occurs.

Power Manager will always quit running applications that might block log out. This is useful but the risk of data loss can be unacceptable. Running a script just before the log out gives you the opportunity to save changes and perform critical steps to save guard work.

In an ideal world, an unattended Mac would not be left with unsaved work on it. But as we do not live in an ideal world, let's walk through running a simple shell script before logging out.

There are two parts to this recipe. We first create the inactivity triggered log out event. Then we extend the event to add the script.

Create an Inactivity Triggered Log Out

First let's create a log out after inactivity event using the Schedule Assistant's User event after inactivity task.

  1. Launch Power Manager.app

    Launch Power Manager on macOS

  2. Select Add or File > New… > New Event… and User event after inactivity from the Schedule Assistant.

    Select the User event after inactivity task

  3. Select Log Out for What should be performed and enter the required amount of inactivity needed before automatically logging out.

    Select log out and set the required inactivity

  4. Continue passed the Time Constraints step.

    Continue passed the Time Constraints

  5. Continue passed the Interactive Constraints step.

    Continue passed the Interactive Constraints

  6. In the Why step name and describe your event.

    Name and document your new event

  7. Continue and Add your new warning event.

    Continue and add your event

    Inactivity trigger script and log out

Your new inactivity triggered log out event has been created. This event will automatically log out an inactive Mac.

Extend the Event to Run a Script

In the next part, we will extend the event to add a run script action just before the log out occurs. To do this we will use the built-in Power Manager Event Editor.

  1. Open the event in the event editor; hold down the Option key and double-click the event.

    Open the event in the event editor

  2. Select Add an action > Run Script > Shell Script in the action section of the event.

    Add a shell script action

    The new action appears below

  3. Drag the new script action above the log out all users actions; actions are performed in order.

    Move the script action above the log out action

  4. Select Edit from the new script action's cog pop-up menu.

  5. Copy and paste in your shell script.

    Edit the script action

  6. Apply and Save your changes to the event.

    Apply changes to the action

    Save changes to the event

The event is now complete. Power Manager will run the actions in order after the required period of inactivity is reached.

Be aware that in this example the shell script will be running in a cron like environment. We recommend using full paths to executables and not replying too heavily on environment variables.

Running AppleScripts

If you want to use an AppleScript action, be sure use set the action's Short User Name to PME:active user. AppleScript requires a graphical user session and this setting will provide one.

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

Published by Graham Miln on