How to Run a Shell Script After Inactivity

Learn how to run a shell script when your computer has been left inactive.

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:

  1. Launch Power

  2. Click Add… to create a new event.

    Click Add to create a new event

  3. Select Run a script after inactivity for the task.

    Select the task Run a script after inactivity

  4. Continue to the Script step.

  5. Copy and paste in your script, remembering to include a hash bang line.

    Copy and paste in the shell script

    For this recipe we will ask the computer to speak out about being left alone:

    say "I have been left unused."
    exit 0
  6. Continue to the After Inactivity step.

  7. Adjust the period of inactivity to wait, as needed.

    Set the hours, minutes, and seconds of desired inactivity

  8. Continue through the Time Constraints and Interactive Constraints steps.

    Continue through the Time Constraints step

    Continue through the Interactive Constraints step

  9. Continue to the Why step and provide a name and any notes.

    Enter an event name and description in the Why step

  10. Continue and Add your new event.

Add your new inactivity triggered event

Your new event is complete

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.