Skip to main content

Run a script on macOS on a schedule using a LaunchAgent

I was setting up a script to run on a nightly schedule. Based on a combination of bits I found on Google, I created the following plist file which I saved in ~/Library/LaunchAgents/net.alexwlchan.run_scheduled_task.plist:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
 <plist version="1.0">





To register the task so it runs in the background:

$ launchctl load net.alexwlchan.run_scheduled_task

You can also unregister the task with unload:

$ launchctl unload net.alexwlchan.run_scheduled_task

When I was working out the XML syntax, I ended up with a few cases where I had a broken task that was “loaded”, and running launchctl load would throw an error. I had to unload; load to get the new task working.

Or run it as a one-off task to check you’ve configured it correctly:

$ launchctl start net.alexwlchan.run_scheduled_task