In this recipe we will run a shell script whenever the computer has been left inactive. We will use Power Manager to achieve this. Using this technique you can automate a wide range of responses to an idle Mac. A shell script could quit computationally expensive processes, clean up temporary files, or hook into an existing workflow.
Create an Inactivity Triggered Shell Script
Let's create a new inactivity triggered event with an embedded shell script:
- Launch Power Manager.app.
Click Add… to create a new event.
Select Run a script after inactivity for the task.
- Continue to the Script step.
Copy and paste in your script, remembering to include a hash bang line.
For this recipe we will ask the computer to speak out about being left alone:
#!/bin/sh say "I have been left unused." exit 0
Continue to the After Inactivity step.
Adjust the period of inactivity to wait, as needed.
Continue through the Time Constraints and Interactive Constraints steps.
Continue to the Why step and provide a name and any notes.
Continue and Add your new event.
Your new inactivity triggered shell script is available immediately. Be sure to test the event works as expected by leaving your computer untouched for 15 minutes – or for whatever period of inactivity you choose.
Combining Inactivity Triggered Scripts
You can use this approach to trigger different scripts at different stages of inactivity. Consider creating multiple inactivity triggered scripts to perform at 5 minutes, then after 10 minutes, and finally after 30 minutes of inactivity. This is all possible with Power Manager.