MacOS – Launchd Wont Run Application

command linelaunchdmacos

I made an app (tested it works) and I want to make it run every day at midnight. Here is my PLIST file:

EDIT: after using @user3439894's code here is what I put in my PLIST

<?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.exampled</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/open</string>
        <string>-a</string>
        <string>/Applications/Calculator.app</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>0</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
    </array>
</dict>
</plist>

When I run it using sudo launchctl load /Users/fbisurveillancevan/Desktop/LaunchMessageText.plist, it returns nothing. When I try again, it returns launchctl: already loaded The daemon/agent appears to be loaded, but does not run

Best Answer

In addition to the comments I've made to your OP, use this example to fix the issues you are having.

  • Note: This was done under macOS High Sierra and if using macOS Catalina there are some changes to the example, which are detailed is a note further below.

As a test, in Terminal, I executed the following commands:

cd ~/Library/LaunchAgents
touch com.example.exampled.plist
open -e com.example.exampled.plist

Copy and paste the following example PLIST XML code into the opened com.example.exampled.plist file, setting the StartCalendarInterval for a few minutes from now, and then save and close it.

<?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.exampled</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/open</string>
        <string>-a</string>
        <string>/Applications/Calculator.app</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>0</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
    </array>
</dict>
</plist>


Back in Terminal, execute:

launchctl load com.example.exampled.plist

Now Calculator will open at the set time, which is set using 24 Hour time, e.g 0 through 23 for the Hour key.


macOS Catalina Notes:

  • In a clean install of macOS Catalina, ~/Library/LaunchAgents doesn't exist by default. Use the following command before cd ~/Library/LaunchAgents:

    mkdir -p ~/Library/LaunchAgents
    
  • Due to changes made by Apple in macOS Catalina the fully qualified pathname for the Calculator is: /System/Applications/Calculator.app

Change:

<string>/Applications/Calculator.app</string>

To:

<string>/System/Applications/Calculator.app</string>

Or as an alternative, just use:

<string>Calculator</string>
  • Both methods work.

Additional testing was done with an application bundle (myApp.app) created by me, saved in /Applications on macOS Catalina, and the following values worked for me:

<string>/Applications/myApp.app</string>

Or:

<string>myApp</string>

Note: The use of sudo is required when dealing with agents and daemons that are in the designated locations other then ~/Library/LaunchAgents in order to create, modify, change permissions to make read-only, delete, etc. the XML Plist file itself. Also note that when using the launchctl command for daemons, sudo may be required in some cases, however do not use sudo to load agents with launchctl, as it will load them as a daemon.

To review the manual pages, in Terminal, use man command where command is the commands name, e.g. man launchctl, or to more easily read the manual pages, just type the commands name and right-click on it selecting: Open man Page

If you are going to use Launch Agents and Launch Daemons, I highly recommend you read the manual pages for launchctl, launchd.plist and launchd.


Per the manual pages for launchctl and launchd.plist the PLIST files for Launch Agents and Launch Daemons belong in specific directories:

FILES
    ~/Library/LaunchAgents          Per-user agents provided by the user.
    /Library/LaunchAgents           Per-user agents provided by the administrator.
    /Library/LaunchDaemons          System-wide daemons provided by the administrator.
    /System/Library/LaunchAgents    Per-user agents provided by OS X.
    /System/Library/LaunchDaemons   System-wide daemons provided by OS X.

There is also an example xml property lists file at the end of the manual page forlaunchd.plist.