Use launchd to run scripts on Mac

  • data-coding
  • mac
  • spring


  • From The Missing Semester:

    You are probably already familiar with the notion of daemons, even if the word seems new. Most computers have a series of processes that are always running in the background rather than waiting for a user to launch them and interact with them. These processes are called daemons and the programs that run as daemons often end with a d to indicate so. For example sshd, the SSH daemon, is the program responsible for listening to incoming SSH requests and checking that the remote user has the necessary credentials to log in.


  • launchd runs scheduled jobs after the computer wakes up, cron does not (but it can be useful sometimes)
  • launchd differentiates between agents and daemons. The main difference is that an agent is run on behalf of the logged in user while a daemon runs on behalf of the root user or any user you specify with the UserName key.
  • If you see </true> in plist, it specifies value for boolean key, like RunAtLoad
  • Commands from here
    • Make sure your filename ends in .plist, e.g. /Users/johnny/Library/LaunchAgents/com.appleseed.runthings.plist
    • Check the syntax of the file: plutil /Users/johnny/Library/LaunchAgents/com.appleseed.runthings.plist
    • Unload and reload your service:
      • launchctl unload /Users/johnny/Library/LaunchAgents/com.appleseed.runthings.plist
      • launchctl load /Users/johnny/Library/LaunchAgents/com.appleseed.runthings.plist
    • Force load in case your job has been disabled: launchctl load -w /Users/johnny/Library/LaunchAgents/com.appleseed.runthings.plist
  • To check if it's running, do launchctl list | grep com.appleseed.runthings
  • If you don't see status 0, see help on errors via launchctl error <error code>
  • Some of my previous errors and their solution:
    • error 78:
      • these solutions lead to this launchd plist generator to fix
      • Essentially, you better run any scripts (even bash) through ProgramArguments and add by what it should be run
      • error 126:
        • An return code status of 126 signifies that the command you are trying to use is available, but not executable.
        • solution
      • Python functions not recognised
        • change PATH same as here