Search code examples
macoscocoalaunchd

When the launchd will register for the events?


I need to open my application and run a specific task from that application automatically in a given time. So I searched in google and came up with launchd. First question, Is this is correct choice?
I have created a plist and I kept that in the location ~/Library/LaunchAgents/fileName.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.companyname.example</string>
    <false/>
    <key>RunAtLoad</key>
    <true/>
    <key>UserName</key>
    <string>mac_1</string>
    <key>Program</key>
    <string>open</string>
    <key>ProgramArguments</key>
    <array>
        <string>Argument_1</string>
        <string>Argument_2</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Day</key>
        <integer>1</integer>
        <key>Hour</key>
        <integer>10</integer>
        <key>Minute</key>
        <integer>41</integer>
        <key>Month</key>
        <integer>8</integer>
        <key>Weekday</key>
        <integer>3</integer>

    </dict>
       <key>KeepAlive</key>
    <true/>
</dict>
</plist>  

The Problem with this is, It is working only when the computer will restarted, otherwise it will not be invoked. So, My Second question when the launchd will register for the events ?


Solution

  • There is a utility called "launchctl" You need to use it to load your plis file into launchd. Type: launchctl load plist_file.

    If you want to schedule something from your application - one way would be calling "system" to execute launchtl utility. There is also undocumented API for launchd but I couldn't find a way to use it for scheduling.

    launchd would be the easiest way to schedule something on the system, as it has reach scheduling properties.