How to Schedule a launchd Job

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

  1. Open Power Manager.app.
  2. Click Add… to create a new event.
    Click Add… to create a new event Click Add… to create a new event
  3. Select Run a script daily and click Continue.
    Select the Run a script daily task Select the Run a script daily task
  4. 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
    Copy and paste in the launchd shell script Copy and paste in the launchd shell script
  5. In the Script step, change the Environment to Super User (root).
  6. Continue to the When step.
  7. In the When step, adjust the time and days as desired.
    Adjust the time and days of the week to trigger Adjust the time and days of the week to trigger
  8. Continue twice, passing the Constraints step.
    Skip the interactive constraints step Skip the interactive constraints step
  9. In the Why step, name and describe your new event.
    Name and describe your new event Name and describe your new event
  10. Continue and Add your new event.
    Confirm the event to create 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 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

  1. Option + double-click your event to open the Event Editor.
    Option + double-click the event Option + double-click the event
  2. Click Add an action to add a Wait action.
    Add a Wait action to the event Add a Wait action to the event
  3. 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 Edit the Wait action to 3:00:00
  4. Apply changes to the Wait action.
    Apply changes and note description change Apply changes and note description change
  5. 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 Duplicate the first Run Script action
  6. 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
    Edit the second shell script Edit the second shell script
  7. Apply changes to the Run Script action.
  8. Save changes to your extended event.
    Apply and save changes to finish the event Apply and save changes to finish the 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.

This article was posted in , , and and tagged , , and .

Published by Graham Miln on