Use Launchd to Schedule Run Scripts on Mac

Automating duties by scheduling scripts is a strong functionality that many directors depend on daily. Common Mac customers may also faucet into this energy utilizing launchd, Apple’s most popular instrument for activity automation and administration on macOS. From scheduling easy duties like turning off Wi-Fi at a particular hour, to operating advanced system backups, launchd on Mac may help you automate your workflows, save time, and be sure that your system runs simply the way in which you prefer it.
What Is Launchd?
Similar to an orchestra requires a conductor to information and harmonize the various devices, macOS Ventura, with its myriad of processes and providers, depends on launchd to make sure every thing performs in excellent live performance. As the primary course of launched by the macOS kernel whenever you boot up your pc, launchd takes heart stage, orchestrating each subsequent course of, service, and utility, very similar to a conductor signaling the start of a symphony with the preliminary baton elevate.
Past its position in system orchestration, launchd can be utilized to schedule scripts, a collection of instructions written to carry out a particular activity. That is performed utilizing the launchctl
command, which serves because the interface for customers to speak and direct the conductor that’s launchd.
Good to know: downloading torrents? Increase effectivity by downloading torrents with Terminal in your Mac.
Daemons and Brokers
launchd is usually known as a daemon, a pc program that runs as a background course of and sometimes isn’t designed to be instantly managed by a person. So far as daemons go, launchd is particular, because it’s the maestro of all different macOS daemons, and it may determine once they begin and cease. These subservient daemons run beneath the foundation person, to allow them to do absolutely anything.
Nonetheless, as a person inquisitive about activity scheduling, operating scripts beneath the foundation person isn’t all the time fascinating or vital. That is the place brokers come into play. Brokers run on behalf of a logged-in person, providing a extra restricted setting and making certain that scripts or duties are carried out with the permissions and preferences of that particular person. As an example, if you want a script to run that adjustments settings or accesses recordsdata inside your account, you’ll use an agent.
Tip: operating into compatibility points with a few of your favourite longstanding Mac apps? Uncover tips on how to set up 32-bit Linux on an previous Mac.
Writing Scripts
To run brokers or daemons by way of launchd, you’ll want to write down some scripts. The most typical scripting language is bash. If you wish to study extra about bash scripting, you possibly can try our newbie’s information to bash scripting.
Your launchd scripts can dwell in two totally different places, relying on whether or not they’re meant to be run as brokers or daemons:
- For these scripts meant to be brokers, performing on behalf of the logged-in person, they need to be saved in “~/Library/LaunchAgents.”
- Conversely, scripts meant to operate as daemons, working system-wide whatever the logged-in person, belong in “/Library/LaunchDaemons.”
Bear in mind, brokers don’t have root permissions, to allow them to’t carry out duties that require deep system entry. Daemons, then again, run with root permissions and might deal with duties that have an effect on the complete system.
Good to know: you possibly can acquire deeper management over your Mac by enabling Root Consumer, permitting you to entry its total file system.
Job Descriptions

Scripts in launchd are triggered by job definitions, that are .plist recordsdata saved in particular directories. These XML recordsdata give the job a reputation, specify the script that must be launched, and point out when the script must be run. When you’ve written your script, you’ll write and cargo a job definition that launches the script on the acceptable time. A job definition seems one thing like this:
<?xml model="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist model="1.0"> <dict> <key>Label</key> <string>native.restart</string> <key>Program</key> <string>/Customers/person/Scripts/restart.sh</string> <key>RunAtLoad</key> <true/> </dict> </plist>
Modify as vital, then put it in a textual content file with the .plist extension earlier than dropping it within the appropriate listing (see above).
There are a couple of key components to the job description:
- Label: the title of the job inside launchd. Have to be distinctive for every job. These are written in reverse area notation, and “native” is a good area for personal brokers.
- Program: the total path of the script this job description launches.
- RunAtLoad: describes when the script must be run. There are a couple of totally different choices right here:
- RunAtLoad: run as quickly because the job definition is loaded. Runs solely as soon as per load.
- StartInterval: begin the job each n seconds. This instance will run the job each 7200 seconds or each 2 hours.
<key>StartInterval</key> <integer>7200</integer>
- StartCalendarInterval: run the job at a particular time and date. The under code will run the job daily at 9 AM.
<key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>9</integer> <key>Minute</key> <integer>0</integer> </dict>
Tip: want extra space for these script recordsdata? Learn to clear the cache in your Mac to rapidly unencumber area.
Loading jobs into launchctl

When you’ve created your scripts and saved your agent in the appropriate place, you’ll must load it into launchctl
. This may occur robotically on logins sooner or later.
To see what’s presently operating in laucnhctl, you should use launchctl checklist
within the terminal. This big checklist might be grepped on your script by labeling it with one thing like the next:
launchctl checklist | grep native.restart
To load a script, open Terminal, and use the next command:
launchctl load ~/Library/LaunchAgents/native.restart.plist

To take away the script from the launchctl queue, use the unload
command:
launchctl unload ~/Library/LaunchAgents/native.restart.plist

Loading a job places it into the launchd queue, and the job will run on the time laid out in its launch circumstances. If you wish to run a script instantly it doesn’t matter what, it’s best to use the “begin” command:
launchctl begin native.restart
This command takes the job’s label and can solely work if the job has already been loaded into launchctl
.
Tip: turn out to be a Mac energy person through the use of Raycast or Alfred to rapidly launch your favourite apps.
Often Requested Questions
How can I verify if launchd has began a script?
You should utilize the launchctl checklist
command within the terminal. This may show all of the loaded jobs. To discover a particular script or job, use grep
, e.g., launchctl checklist | grep your_script_name
.
What if launchd is utilizing too many system sources?
If launchd is consuming extreme sources, it’s normally on account of a misbehaving script or job. It is best to assessment the scripts you’ve added lately, and unload them utilizing launchctl unload /path/to/job.plist
.
What is the distinction between cron and launchd?
Each cron and launchd are scheduling providers, however they function in a different way. cron is an older Unix-based job scheduler that runs jobs at fastened occasions or intervals outlined in a crontab file. launchd is Apple’s newer system for macOS that may begin jobs based mostly on numerous triggers – not simply time.
Can I take advantage of different scripting languages in addition to bash with launchd?
launchd can execute any script that may be run from the terminal. This consists of scripts written in Python, Perl, Ruby, and different languages.
Picture credit score: Pexels. All screenshots by David Morelo.