Working With Other Scheduling Tools

Power Manager has been designed to work well with other tools. Where Power Manager reaches the limits of its functionality, we ensured we offered a well-defined and documented interface. Through these interfaces Power Manager can talk to, and listen to, other tools.

We want Power Manager to be a great general purpose scheduling tool. However, we appreciate that other tools can and should be used alongside Power Manager when creating your energy saving schedules.

A couple of beloved tools come to mind: launchd and cron.

launchd

launchd (Launch Daemon) is a core low level component of Mac OS. launchd is open source and managed by Apple, Inc. launchd is often used as a scheduling tool by administrators.

The design principal behind launchd differs from Power Manager. launchd is designed as a watchdog tool for host, user, and session wide services. launchd’s primary concern is ensuring critical services are available as needed. No attempt is made to be a user level tool.

To consider Power Manager a replacement, or alternative, to launchd risks missing the vital role launchd plays in Apple’s operating systems. Power Manager itself is launched and watched over by launchd.

Power Manager adds a layer above launchd; a layer where the user’s schedule is the primary concern.

Triggering an Event from a launchd file

To trigger a Power Manager event from a launchd file, use the pmctl tool. Within your launchd file, call pmctl with the request scheduler.perform, and provide the desired event identifier as the parameter.

Sample launchd file extract to perform a Power Manager event:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.example.launchd</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/Power Manager.app/Contents/Tools/pmctl</string>
        <string>-netrc</string>
        <string>/path/to/credentials-file</string>
        <string>ondemand.perform</string>
        <string>unique ID=an-event-id</string>
    </array>
</dict>

cron

cron is a stalwart of the UNIX scheduling world. cron is a fairly simple but impressively powerful tool for running commands.

Commands can be set to run when a specific time occurs. cron’s interface and design suits an always on, terminal driven, computing environment from a previous era of UNIX. With modern UNIX variants came graphical interfaces, increased security boundaries, and abstractions freeing the user from needing a terminal.

Despite this move away from the terminal, cron remains popular. It is reliable and the notation cron uses for encoding repeating trigger time instructions is surpassed in expressive power by few other tools.

However, cron has weaknesses. cron makes assumptions that are no longer true:

cron’s scheduler requires processing time each and every minute. Each minute the scheduler checks if a command needs to be run. This simple looping behaviour permits the tool’s complex notation and provides a reliable service.

The need to process the schedule every minute is no longer appropriate. What if the host is powered off, or asleep? If the next scheduled event is six months away, why burden the processor every minute until that time?

launchd was created, in part, to replace cron and similar tools. However, there are still situations where cron is needed, or preferred. For those situations, you can use cron to trigger your Power Manager events.

Triggering an Event from a cron Job

To trigger a Power Manager event from a cron job, use the pmctl tool. Set the cron job command to call pmctl with the request ondemand.perform, and provide the desired event identifier as the parameter.

Below is a cron job to perform a Power Manager event every 15 minutes between 9am - 5pm:

*/15 9-17 * * * root '/Applications/Power Manager.app/Contents/Tools/pmctl' -netrc /path/to/credentials ondemand.perform 'unique ID=your-event-id'

Authentication with netrc

Other tools need to provide credentials to Power Manager. This is handled securely through a netrc formatted file. See .netrc and the manual page netrc(5).

A example netrc file with one set of credentials:

machine the.host.name
login sally
password secretpassword