Home/Guides/How to schedule and auto-start jobs with user-level LaunchAgents on a remote Mac mini

How to schedule and auto-start jobs with user-level LaunchAgents on a remote Mac mini

Updated · airankrush

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

  1. Use only ~/Library/LaunchAgents

    According to Apple's launchd documentation, ~/Library/LaunchAgents holds 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/LaunchAgents and /Library/LaunchDaemons are 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.

  2. 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 -lint followed by the file path.

  3. 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.report

    kickstart runs the job once immediately without waiting for the schedule, which is useful for verifying it. print shows the last exit status. After changing the plist, run launchctl bootout gui/$UID/com.example.report first and then bootstrap again.

  4. 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 through zsh -lc as above and put the PATH setup in ~/.zprofile; or give PATH explicitly in the plist with EnvironmentVariables.

  5. 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.

  6. Use KeepAlive for always-on processes instead of wrapping them in another loop

    For a program that should stay online, replace StartCalendarInterval with <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.

  7. 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.

Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support

Back to home · All guides