Skip to content

Launchd

leslie helou edited this page Jun 24, 2025 · 2 revisions

Running from a Launch Daemon

The API Utility app can be configured to run through a launch daemon to perform a scheduled task.

Launch the app with elevated privileges.

sudo /Applications/API\ Utility.app/Contents/MacOS/apiutil

Add the desired target(s).

Once added you can verify the required credentials exist in the System keychain

We're now able to utilize API Utility.app from a launch daemon. As an example let's use one of the example scripts to export all computer extension attributes and run it nightly at 1:00 am.

Create a directory to store scripts used by API Utility:
sudo mkdir -p /usr/local/bin/apiutilScripts

Save the script to:
/usr/local/bin/apiutilScripts/exportComputerEAs.sh

Set permissions on the script:
sudo chmod 744 /usr/local/bin/apiutilScripts/exportComputerEAs.sh

Create the launch daemon:

<?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>apiutil.exportComputerEAs</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/apiutilScripts/exportComputerEAs.sh</string> 
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>1</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    <key>RunAtLoad</key>
    <false/>
    <key>StandardOutPath</key>
    <string>/var/log/exportComputerEAs_out.log</string>
    <key>StandardErrorPath</key>
    <string>/var/log/exportComputerEAs_err.log</string> 
</dict>
</plist>

Save the launch daemon, say to the Desktop, as apiutil.exportComputerEAs.plist, Note the filename matches (less the .plist) the label in the launch daemon.

Move the file:
sudo mv ~/Desktop/apiutil.exportComputerEAs.plist /Library/LaunchDaemons/

Ensure we have the proper owner and group set on the file:
sudo chown root:wheel /Library/LaunchDaemons/apiutil.exportComputerEAs.plist

Set permissions on the file:
sudo chmod 644 /Library/LaunchDaemons/apiutil.exportComputerEAs.plist

Load the launch daemon:
sudo launchctl bootstrap system /Library/LaunchDaemons/apiutil.exportComputerEAs.plist

The export script is now set to run at 1:00 am

For quick testing we can set the script to run when the launch daemon is loaded, in addition to its scheduled time. Simply change the value for the key RunAtLoad from false to true:

<key>RunAtLoad</key>
    <true/>

Unload the launch daemon:
sudo launchctl bootout system /Library/LaunchDaemons/apiutil.exportComputerEAs.plist

Load the launch daemon, as we did above. You check the results of the script by viewing the logs:
/var/log/exportComputerEAs_out.log
And for potential errors:
/var/log/exportComputerEAs_err.log

Revert the changes if you don't want to run the script whenever the launch daemon is loaded, like after a reboot.

Clone this wiki locally