What this guide covers
When you finish, your script runs on a schedule or starts automatically after login, and its output goes to a log. You need: a script that already runs correctly by hand in the terminal.
Steps
Use only ~/Library/LaunchAgents
According to Apple's launchd documentation,
~/Library/LaunchAgentsholds third-party agents that apply only to the logged-in user. It is the only location you should use, and writing to it needs no administrator password./Library/LaunchAgentsand/Library/LaunchDaemonsare for all users and the system level, and the platform's management and remote access components may have entries there. Do not add anything to them, and certainly do not delete or edit the files already in them.Write a plist
Create
~/Library/LaunchAgents/com.example.report.plist(replace the label with a name of your own):<?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.report</string> <key>ProgramArguments</key> <array><string>/bin/zsh</string><string>-lc</string><string>~/bin/report.sh</string></array> <key>StartCalendarInterval</key> <dict><key>Hour</key><integer>9</integer><key>Minute</key><integer>0</integer></dict> <key>StandardOutPath</key><string>/tmp/report.out.log</string> <key>StandardErrorPath</key><string>/tmp/report.err.log</string> </dict> </plist>Log paths have to be absolute. We suggest changing them to a log folder under your user directory. When you are done, check the syntax with
plutil -lintfollowed by the file path.Load it, run it once now, and check its status
The system manual recommends the newer subcommands over the legacy load/unload:
launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.example.report.plist launchctl kickstart -k gui/$UID/com.example.report launchctl print gui/$UID/com.example.reportkickstartruns the job once immediately without waiting for the schedule, which is useful for verifying it.printshows the last exit status. After changing the plist, runlaunchctl bootout gui/$UID/com.example.reportfirst and then bootstrap again.Pitfall 1: PATH inside launchd is very short
The job is not started from your terminal, so it knows nothing about the Homebrew, Node and Python paths configured in
~/.zshrc. The typical symptom is that the script works when run by hand and reports a command not found when run on schedule. Choose one of three fixes: write every command with its full path (starting with/opt/homebrew/bin/, for example); start throughzsh -lcas above and put the PATH setup in~/.zprofile; or give PATH explicitly in the plist withEnvironmentVariables.Pitfall 2: times follow this machine's time zone
Check the machine’s time zone in System Settings → General → Date & Time. Use the city or time zone in your delivery details. The United States has multiple time zones, and some observe daylight saving time. Record the time zone used by logs and scheduled jobs.
Use KeepAlive for always-on processes instead of wrapping them in another loop
For a program that should stay online, replace
StartCalendarIntervalwith<key>KeepAlive</key><true/>, and launchd starts the process again after it exits. The manual mentions that launchd applies a throttle interval to frequent restarts, so a program that crashes on startup will not respawn without limit, but failure records will keep appearing in the log, so remember to look. The program itself has to run in the foreground and must not put itself into the background.After a restart: jobs run only once you have logged in
User-level agents load only after your account logs in to a graphical session. If the machine restarts and nobody logs in, none of your scheduled or auto-start jobs run. After every restart, log in once through the remote desktop and spot-check one job. Recovering automatically after a restart without logging in involves automatic login and disk encryption configuration, so ask support to assess it first. See recovering after a restart.
FAQ
Can I use cron?
macOS still includes cron, and it works for simple cases. launchd is the system's native mechanism and supports restarting after a crash and catching up after sleep, so it suits long-running jobs better.
My job reads a secret from the keychain. Why does it sometimes fail?
The login keychain unlocks only after you log in to the desktop. The job does not run before then, so normally there is no problem. If you trigger it by hand from a remote command line session without having logged in to the desktop, it may not be able to read the secret.
Can I create a system-level daemon that runs at boot without a login?
Writing to the system-level directories needs elevated privileges, and the platform has entries there, so we do not recommend doing it yourself. Tell Contact support what you need and they will assess what is feasible.
How do I remove a job completely?
Unload it with launchctl bootout gui/$UID/<label>, then delete the matching plist file. Before the rental ends, remove every job you added this way.
Related
- How to keep long tasks running on a remote Mac mini after you disconnect
- Can I still connect to a remote Mac mini after it restarts?
- How do I know my unattended agent is still running properly?
- How to keep a remote Mac mini awake during a task with caffeinate
- How to set up zsh on a remote Mac mini: config files, prompt and history
Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support