Run a Root Script at Boot or on a Schedule With a macOS LaunchDaemon
Run a bash script as root at boot or on a timetable with a LaunchDaemon: plist keys, owner and mode, launchctl bootstrap, print, kickstart and bootout rollback.

To run a script as root at boot or on a timetable, put a property list in /Library/LaunchDaemons, name it after its Label, make it owned by root and not writable by group or others, then load it with sudo launchctl bootstrap system and the path to the plist. launchd runs whatever ProgramArguments names, as root, every time the schedule fires. The key constraint is that file permission: launchd refuses a daemon plist that is not owned by root or that group or others can write, because anyone who can edit that file can choose what runs as root. Use StartCalendarInterval for a timetable and RunAtLoad only when the job really has to run at boot. Remove the job with sudo launchctl bootout system/com.example.disk-report.
The rest of this page builds one small daemon: a bash script, its plist, the checks before loading, the proof that it ran, and how to take it out again cleanly.
#Why the usual workaround breaks on a modern Mac
Recurring root tasks on Macs tend to arrive by accident. Someone adds a line to root’s crontab on one machine, or keeps a script on a shared drive and runs it with sudo when a ticket comes in. Older guides still show launchctl load and launchctl unload. Each of these works once and then drifts. A crontab entry is easy to forget on the one Mac that has it. A script run by hand only runs when a person remembers. And load and unload are now legacy subcommands: Apple’s launchctl manual lists bootstrap, bootout, enable and disable as their recommended replacements.
Timing matters too. A Mac that sleeps overnight misses cron jobs scheduled while it slept. launchd handles that case differently: if the time in StartCalendarInterval passes while the Mac is asleep, launchd starts the job the next time it wakes, and several missed runs are merged into one.
#The better workflow: one plist, one label, one log
A LaunchDaemon is a property list that launchd reads into the system domain, the privileged context where jobs run as root. Every job has a Label, which must be unique, and the convention is to name the file <Label>.plist. That one label then names the job everywhere: in launchctl print system/<label>, in launchctl bootout, and in the label rules a management profile can use to approve it.
The job itself should do its work and exit. Apple’s launchd.plist manual says a job launched by launchd must not call daemon(3) or fork and let the parent exit, and that it should handle SIGTERM. For a shell script, that means no backgrounding the main work with & and no wrapper that exits early. Output goes to the files named in StandardOutPath and StandardErrorPath, which launchd creates if they do not exist, so every run leaves evidence you can read later.
Choose how the job starts with one key, not several:
StartCalendarIntervalruns it at a calendar time, withMinute,Hour,Day,WeekdayandMonthkeys; any key you leave out is a wildcard, and an array of dictionaries gives several times.StartIntervalruns it every so many seconds, but an interval that falls while the Mac is asleep, or while the job is still running, is simply missed.RunAtLoadruns it once when the job is loaded, which for a daemon in/Library/LaunchDaemonsmeans at startup. It defaults to false, and Apple’s manual asks you to avoid it because speculative launches slow boot and login.
Leave KeepAlive out of a script that is meant to finish. It defaults to false; setting it implies RunAtLoad and tells launchd to restart the job, which turns a nightly report into a loop that launchd throttles to one start every 10 seconds by default.
#Setting up the daemon step by step
This example writes a nightly report job labelled com.example.disk-report. Replace the label with your organisation’s reverse-DNS name and the script body with the real work. Do the first run on a test Mac you can reach directly, not on a user’s machine. Before you start, check whether something with the same label already exists, so you do not overwrite it:
1ls -l /Library/LaunchDaemons/com.example.disk-report.plist
2launchctl print system/com.example.disk-reportbash -n reads the script without running it and reports syntax errors. No output means it parsed. If anything is printed, fix the script before going on.
Write the plist. Save it as /Library/LaunchDaemons/com.example.disk-report.plist. ProgramArguments calls /bin/bash with the script path, so the job does not depend on the script’s execute bit, and the first element is an absolute path. The schedule runs it at 02:30 every day.
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3<plist version="1.0">
4<dict>
5 <key>Label</key>
6 <string>com.example.disk-report</string>
7 <key>ProgramArguments</key>
8 <array>
9 <string>/bin/bash</string>
10 <string>/Library/Scripts/example-org/disk-report.sh</string>
11 </array>
12 <key>StartCalendarInterval</key>
13 <dict>
14 <key>Hour</key>
15 <integer>2</integer>
16 <key>Minute</key>
17 <integer>30</integer>
18 </dict>
19 <key>StandardOutPath</key>
20 <string>/Library/Logs/com.example.disk-report.log</string>
21 <key>StandardErrorPath</key>
22 <string>/Library/Logs/com.example.disk-report.err</string>
23</dict>
24</plist>Lint it, then set owner and mode.
1plutil -lint /Library/LaunchDaemons/com.example.disk-report.plist
2sudo chown root:wheel /Library/LaunchDaemons/com.example.disk-report.plist /Library/Scripts/example-org/disk-report.sh
3sudo chmod 644 /Library/LaunchDaemons/com.example.disk-report.plist
4sudo chmod go-w /Library/Scripts/example-org/disk-report.sh
5ls -l /Library/LaunchDaemons/com.example.disk-report.plist /Library/Scripts/example-org/disk-report.shplutil -lint should report the file as OK. ls -l should show root as owner and no w in the group or other positions. Mode 644 makes the plist readable by anyone and writable only by its owner; go-w removes group and other write from the script. Stop here if the lint fails: launchd will not load a file it cannot parse.
Load it into the system domain.
sudo launchctl bootstrap system /Library/LaunchDaemons/com.example.disk-report.plistSilence means it loaded. If there is a problem, launchctl prints the path with the error. A numeric error can be translated with launchctl error followed by the number. Do not retry in a loop; read the error, fix the file and try once more.
Run it once now instead of waiting for 02:30.
sudo launchctl kickstart -p system/com.example.disk-reportkickstart runs the job immediately regardless of its schedule, and -p prints the process ID. The schedule is unaffected; the job still runs at 02:30.
Once the job is loaded, backing it out is always the same: sudo launchctl bootout system/com.example.disk-report, then move the plist out of /Library/LaunchDaemons. Before loading, nothing is running, and you only need to move the files away. Both are covered in full further down.

#Who can change the daemon, and what it may run
A LaunchDaemon is a standing grant of root. Apple’s manual is blunt about why the ownership rule exists: write access to a launchd configuration file lets someone choose which executable is launched. Treat the plist, the script and the folder that holds the script as one unit with the same protection, even though launchd itself only checks the plist. A root-owned plist that runs a script any admin user can edit is still a root shell for that user.
- Keep the plist owned by root with mode 644, and the script owned by root and not writable by group or others.
- Never put your own files in
/System/Library/LaunchDaemons. That folder is for macOS, and thelaunchctlmanual warns that allowing non-root write access to it will make the Mac unbootable. - Add
UserNameonly when the work does not need root. It applies only to jobs in the system domain, and running as a less privileged account reduces what a mistake in the script can damage. - Keep secrets out of the plist.
EnvironmentVariablesis readable by anyone who can read the file, and mode 644 means everyone can.
If your Macs are managed, a device management configuration can approve background items automatically by their exact launchd Label or by a label prefix.
#Prove the job ran
A loaded job is not the same as a working one. Check three things after the kickstart, and again the morning after the first scheduled run.
1launchctl print system/com.example.disk-report
2cat /Library/Logs/com.example.disk-report.log
3cat /Library/Logs/com.example.disk-report.errlaunchctl print shows where the job was loaded from on disk, its current state and its last exit status. The origin should be your plist path, and the last exit status should be 0. Apple states plainly that this output is not an API and can change between releases, so read it, but do not build monitoring that parses it. The log file should contain both disk-report lines from the script, and the error file should be empty or absent.
#Where the job appears on the Mac
On macOS 13 and later, background task management makes launch daemons visible to the user. Look under Apple menu > System Settings > General > Login Items & Extensions, where App Background Activity lists what is allowed to run in the background, and the Information button shows the item being launched. For a text record that is easier to search, run:
sudo sfltool dumpbtmIt prints the status of every login and background item. Search the output for your label. If a job that worked yesterday stops, check here early: an item switched off in App Background Activity is not allowed to run.

#Where it goes wrong
- Bootstrap refuses the plist. The file is owned by your admin account or is group-writable, often because it was written in your home folder and moved into place, which keeps its original owner. Fix the owner and mode, then bootstrap again.
- The job loads but never seems to run. Check that the times in
StartCalendarIntervalare what you meant:Hourruns 0 to 23, and a missing key is a wildcard, so a dictionary with onlyMinuteruns every hour. - The last exit status is not 0 and the error file says a command was not found. launchd starts jobs with a minimal environment, so call tools by absolute path in the script or set
PATHinEnvironmentVariables. - The job starts and is immediately restarted, over and over.
KeepAlivewas added to a script that exits by design. Remove it. - The work stops halfway. The script put its main task in the background and exited, which launchd treats as the job finishing. Keep the work in the foreground.
- An edit to the plist has no effect. launchd keeps the definition it loaded, so an edited file needs a
bootoutand a freshbootstrap.
For more on reading launchd’s own messages when a job misbehaves around login, see triaging launchd log noise at the login window.
#Roll back with launchctl bootout
Removing the job is two steps and both are reversible. Keep a copy of the plist and script before you change anything, so you can put them back exactly.
1sudo launchctl bootout system/com.example.disk-report
2sudo mkdir -p /Library/Scripts/example-org/disabled
3sudo mv /Library/LaunchDaemons/com.example.disk-report.plist /Library/Scripts/example-org/disabled/bootout removes the job from the system domain; moving the plist out of /Library/LaunchDaemons stops it coming back at the next restart. Confirm with launchctl print system/com.example.disk-report, which should now report that the service cannot be found. To restore, move the plist back, check owner and mode again, and run the same bootstrap command.
To keep the job out without moving any files, run the bootout and then sudo launchctl disable system/com.example.disk-report. A disabled service cannot be loaded again, even at restart, until you run sudo launchctl enable system/com.example.disk-report. Do not use sfltool resetbtm as a rollback: it resets login and background item data for every item on the Mac, not just yours, and Apple recommends a restart after it.
#What to track once it is live
Decide what a good run looks like before rolling out, and check for it instead of waiting for a ticket.
- The last exit status in
launchctl printis 0 on every Mac you sample. - The log file’s modification time is no older than the schedule allows: under a day for a daily job, allowing for Macs that were off.
- The error file stays empty.
- The manual task the job replaced has stopped producing tickets. For a cleanup job, the low-disk cleanup playbook shows the ticket pattern to watch.
Review the job each time macOS has a major release. Background item handling has changed in recent versions, and the launchctl print format can change without notice.
#Before you go live on more Macs
- The label is unique, reverse-DNS, and matches the plist file name.
bash -nandplutil -lintboth pass.- The plist and script are owned by root and not writable by group or others.
- The script runs in the foreground, exits with 0 on success and calls tools by absolute path.
- Only one start key is set, and
KeepAliveis absent. - A kickstart on a test Mac gave exit status 0 and a log entry.
- The first scheduled run on that Mac also succeeded.
- The job appears in
sfltool dumpbtmand you know what users will see in Login Items & Extensions. - The bootout and restore steps were tried once on the test Mac.
Once all of that holds on one Mac, the same files can go to the rest through your usual packaging. If users keep switching background items off, the approach in approving background tasks before users see the prompt applies to your own labels too.
About this page
Credited to
Dates
Published
Sources
Corrections
No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.
Related articles
Automation & Scripting
Stop the WMI Repair Ticket Loop with Proactive Remediation
A corrupted WMI repository silently blocks Software Center and Intune app installs until this proactive remediation script rebuilds it automatically.
ReadArtificial Intelligence
Signal Blocks AI Assistants From Reading Its Screen, and Apple’s macOS 27.3 Beta Makes Full Disk Access Harder to Grant
Signal now blocks screenshots by default so phone AI assistants cannot read its chats, and Apple's macOS 27.3 beta adds friction to Full Disk Access. What Mac and phone admins should test and ask vendors.
ReadSecurity & Operations
Roll Out Microsoft Defender Attack Surface Reduction Rules from Audit to Block Without Breaking Business Apps
Move Defender ASR rules from Audit to Block one ring and one rule at a time, with per-rule exclusions, event checks and a clean rollback.
ReadDiscover more
Ops Playbook
Lexicon Definitions
Was this useful?
Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.
Operate smarter, with fewer recurring tickets.
Receive new operational playbooks, incident-prevention guidance, automation scripts and recovery runbooks.
Comments
Add a thoughtful note on Run a Root Script at Boot or on a Schedule With a macOS LaunchDaemon. Comments are checked for spam and held for moderation before appearing.