Power Manager can schedule an external shell script to run at specific times or on-demand. An external shell script is separate from your Power Manager event and is often an executable file containing a bash, perl, python or ruby script.
We recommend embedding your scripts with Power Manager events. Embedded scripts can not be accidentally moved or changed without realising they are part of a greater workflow. Events with embedded scripts can also be shared easily; the schedule file contains everything needed. See our recipe about creating an event with an embedded script.
Sometimes an external script is the only available option. Let’s walk through how to create a Power Manager event that executes an existing shell script on your Mac.
Creating a Shell Script Event
Launch Power Manager.app.
Click Add… to create a new event.
Click Add… to create a new Power Manager event
Select the Launch an application daily task.
Select the task Launch an application daily
Choose your external shell script for Launch which application?.
Choose the external script for Power Manager to execute
After choosing your script, an Environment popup menu appears. This indicates Power Manager recognises the chosen file is a script and can be executed.
After choosing a script an environment option appears
Continue to the When step.
Adjust the time and date to perform your script.
Adjust the time and days to perform the external script
Continue to the Constraints step.
Continued passed the Constraints step
Continue to the Why step.
Name and describe your new event.
Name and describe the event in the Why step
Continue and then Add your Power Manager script event.
Add the event in the Confirm step
The external shell script event is created and available immediately
Your event is scheduled immediately and will perform at the time you selected in the When step.
Your event is also available on-demand, through the Power Manager status menu and via Power Manager Remote on your iOS device.
A hard disk drive physically connected to your Mac can be either be mounted or unmounted. Only if the drive is mounted can the contents of the drive be accessed. Most of the time you will want all your drives mounted and available to use.
There is a situation where it is better to leave your drive unmounted until needed. This is when a drive is used exclusively for backing up. By limiting when your back up drive can be accessed, you minimise the chances of accidental deletion and mistakes that risk the contents of the drive.
Power Manager is frequently used as part of a back up workflow. Previously we have covered how to mount a network drive using the mount_smbfs command.
Let’s walk through how to unmount a volume as part of a schedule. We are going to use a small shell script and the command line tool diskutil.
The shell script will issue one command:
diskutil unmount "/Volumes/Internal"
This single command will unmount the hard disk drive volume called Internal. For your script, you will need to replace the name Internal with the name of the volume you want to unmount.
Create a Daily Unmount Event
Launch Power Manager.app
Click Add… to create a new event.
Click Add in Power Manager to create a new event.
Select Run a script daily and click Continue.
Select the Run a script daily in the Schedule Assistant.
In the Script step, copy and paste the following into the Script to run:
Power Manager events can perform a wide range of actions to a schedule. Eventually you will want to create an event that only performs if a specific application is or is not running. For many, this is the first step from creating simple events to creating more sophisticated schedules.
The best place to start adding conditions to your events is through the Schedule Assistant. Most tasks have one or two steps dedicated to conditions.
Interactive Conditions
An Interactive Constraints step appears in most tasks offered by the Schedule Assistant. This step includes a range of conditions that can be applied to your events.
Power Manager’s interactive constraints step.
We are going to focus on the Any of the following applications are running: option. With the associated check box enabled, a list of applications and processes becomes available.
By adding applications to this list, you restrict when the event can be performed. Power Manager will trigger the event and then check if any of the listed applications are running.
Power Manager will check not only the active user, but all Fast Switched users as well. This means Power Manager works even if you have left the application running and switched to another user, or even switched back to the Login Window.
For more technical workflows, you can add any executable file to the application list. Power Manager will check for processes as well as bundled applications. This means you can add command line tools like rsync and XPC processes like com.apple.coremedia.videodecoder.
Conditional Shut Down
Let’s walk through creating an event that will shut down your Mac but only if you are not using the computer and Microsoft PowerPoint is not running.
Open Power Manager.
Click Add… to create a new event.
Click add to begin creating the event.
Select Power off after inactivity and click Continue.
Select the Power off after inactivity task.
In the What and When step, adjust What should be performed? to Shut Down.
Select Shut Down for the action to perform.
Click Continue.
In the Time Constraints step, adjust the time range to 10:00pm – 6:00am.
Adjust the time range to cover the night.
Click Continue.
In the Interactive Constraints step, enable Any of the following applications are running: check box.
Add Microsoft PowerPoint to the list of applications in the step. Click Add… or drag and drop the application onto the list.
Enable the check box and add your application.
Click Continue to the Why step and provide an event name.
Provide a name and description of the event.
Click Continue to the Confirm step and Add your event.
Confirm the event should be created.
Your conditional shut down event is created and scheduled.
The conditional shut down event is ready and immediately scheduled. Your Mac will now shut down automatically at night but only if three conditions are met:
you have not used the Mac for 15 minutes;
the time is between 10pm and 6am;
Microsoft PowerPoint is not running.
Adding More Conditions
You are not limited to the steps offered by the Schedule Assistant. Once you have created your event, use the event editor to see how your event is put together and to add more conditions.
To open the event editor, hold down the Option key and double-click on your event.
Conditional event displayed in Power Manager’s event editor.
There are more conditions available through the event editor than are shown in the Schedule Assistant.
launchd manages Mac OS X services. These services include a mix of critical processes, such as the WindowServer, and optional services like screen sharing. We are going to use Power Manager to turn on and off services according to a schedule.
launchd offers some scheduling capabilities. launchd’s scheduling capabilities are directed at managing regular recurring tasks; tasks such as running a daily, weekly, and month log rotation script are managed in Mac OS X by launchd.
launchd’s approach differs from Power Manager becauses its scope and focus differs. Both tools offer well defined interfaces and are complementary; thus the capabilities of both products can be blended to create schedules and services that neither tool excels at individually.
This recipe assumes you are familiar with launchd job tickets and managing jobs from the command line using launchctl. We are going to work with a fictional watchdog focused launchd job ticket notionally located in the Library’s daemons folder /Library/LaunchDaemons/com.example.job.plist.
Let’s create an event that will turn on (enable) a launchd job ticket at 4pm on work days – Monday through Friday. Afterwards we will extend the event to turn off (disable) the same launchd job ticket a couple of hours later. This event will provide a launchd managed service for a couple of hours each work day evening. Outside of this schedule the service will be unavailable.
We will start by following using the Schedule Assistant to create a run shell script event.
Regularly Enable a launchd Job
Open Power Manager.app.
Click Add… to create a new event.
Click Add… to create a new event
Select Run a script daily and click Continue.
Select the Run a script daily task
In the Script step, copy and paste the following into the Script to run:
In the Script step, change the Environment to Super User (root).
Continue to the When step.
In the When step, adjust the time and days as desired.
Adjust the time and days of the week to trigger
Continue twice, passing the Constraints step.
Skip the interactive constraints step
In the Why step, name and describe your new event.
Name and describe your new event
Continue and Add your new event.
Confirm the event to create
At this stage, you have an event that will turn on your launchd job each work day afternoon at 4pm. Left alone the service will turn on and never be turned off.
The first half of the event is ready
We could create a second event to turn off the launchd job ticket. There is however an alternative approach that only requires one event. With both tasks encapsulated in a single event, the turning on and off scripts can be delayed or adjusted as one.
With two events, you would need to adjust or cancel both events should your schedule change.
With one combined event, you only need to adjust or cancel one event when your schedule changes. You will also be able to see if the event is currently being performed – and even be able to make adjustments mid-performance.
Let’s extend the event to wait a few hours after being triggered and then turn off the launchd job.
Extending to Wait and Disable the launchd Job
Option + double-click your event to open the Event Editor.
Option + double-click the event
Click Add an action to add a Wait action.
Add a Wait action to the event
Click the Action cog of the Wait action and edit the duration to read: 3:00:00.
Edit the Wait action to 3:00:00
Apply changes to the Wait action.
Apply changes and note description change
Duplicate the Run a script as root. action and drag it below the Wait action. You can duplicate using the Action cog or by Option + dragging to copy.
Duplicate the first Run Script action
Click the Action cog of the final action and edit the script to read:
The event is now complete. The event will be triggered Monday through Friday at 4pm. At that time a script will enable the launchd job before waiting three hours. After three hours, a second script will disable the launchd job.