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.
- Select Run a script daily and click Continue.
In the Script step, copy and paste the following into the Script to run:
#!/bin/sh launchctl load -w /Library/LaunchDaemons/com.example.job.plist exit 0
- 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.
- Continue twice, passing the Constraints step.
- In the Why step, name and describe your new event.
- Continue and Add your new event.
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.
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.
- Click Add an action to add a Wait action.
- Click the Action cog of the Wait action and edit the duration to read: 3:00:00.
- Apply changes to the Wait action.
- 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.
Click the Action cog of the final action and edit the script to read:
#!/bin/sh launchctl unload -w /Library/LaunchDaemons/com.example.job.plist exit 0
- Apply changes to the Run Script action.
- Save changes to your extended event.
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.