How to Run a Script Before an Inactivity Triggered Log Out
Learn how to run a script just before automatically logging out an inactive Mac.
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.
Launch Power Manager.app
Select Add or File > New… > New Event… and User event after inactivity from the Schedule Assistant.
Select Log Out for What should be performed and enter the required amount of inactivity needed before automatically logging out.
Continue passed the Time Constraints step.
Continue passed the Interactive Constraints step.
In the Why step name and describe your event.
Continue and Add your new warning event.
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.
Open the event in the event editor; hold down the Option key and double-click the event.
Select Add an action > Run Script > Shell Script in the action section of the event.
Drag the new script action above the log out all users actions; actions are performed in order.
Select Edit from the new script action’s cog pop-up menu.
Copy and paste in your shell script.
Apply and Save your 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.