Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

launchd ignores RunAtLoad=false

After having successfully set up a backup job with rsync that is launched with a launchd script, there is only one thing that remains:


launchd runs my script every time it is loaded, i.e. at every startup, instead of just at the specified day and time. I know that the RunAtLoad key default is false, but I still added it to thest whether it helped, but it has not.


What could be the problem?


Here is my launchd plist file (which I located in /Library/LaunchAgents/)


<?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>org.jenal.docs_backup</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/marcus/bin/enterprise_docs_rsync.sh</string>
    </array>
          <key>RunAtLoad</key>
                    <false/>
    <key>StartCalendarInterval</key>
              <dict>
                  <key>Hour</key>
                  <integer>11</integer>
                  <key>Minute</key>
                  <integer>30</integer>
                  <key>Weekday</key>
                  <integer>5</integer>
              </dict>
          <key>KeepAlive</key>
          <dict>
              <key>SuccessfulExit</key>
              <false/>
          </dict>
          <key>StandardOutPath</key>
          <string>/Users/marcus/tmp/launchd_pix.out</string>
          <key>StandardErrorPath</key>
          <string>/Users/marcus/tmp/launchd_pix.err</string>
</dict>
</plist>


Appreciate any help with that!

iMac (21.5-inch Mid 2011), OS X Mountain Lion (10.8.2)

Posted on Jan 28, 2013 11:46 AM

Reply
19 replies

Feb 24, 2013 6:53 PM in response to wondermac

Hello


If I understand it correctly, you want to set {RunAtLoad = false} while {KeepAlive = {SuccessfulExit = false}} to re-run rsync utility in case it exits with error but {RunAtLoad = false} and {KeepAlive != false} do not mix well.


Then I would keep the launchd plist as simple as possible and implement a retry logic in shell script as listed below.


-- shell script


#!/bin/bash

declare -i maxtry=30    # max try count
declare -i interval=10  # retry interval [sec]
declare -i k=0          # try counter
declare -i ret=1        # last status

run_rsync()
{
    SRC="/Volumes/Data/Pictures"
    DEST="rsync@cloud9.local:/volume1/Backups/"
    OPTIONS=(
            -e ssh 
            -E 
            -azvhHS    
            --delete 
            --numeric-ids 
            --log-file="/Users/marcus/tmp/rlog_pix" 
            --exclude-from="/Users/marcus/tmp/exclude.txt"
            )
       
    /usr/local/bin/rsync "${OPTIONS[@]}" "$SRC" "$DEST"
}

until (( ret == 0 || ++k > maxtry ))
do
    run_rsync
    ret=$?
    if (( ret != 0 ))
    then
        sleep $interval
    fi
done
exit $ret



-- launchd 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>org.jenal.docs_backup</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/marcus/bin/enterprise_docs_rsync.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>11</integer>
        <key>Minute</key>
        <integer>30</integer>
        <key>Weekday</key>
        <integer>1</integer>
    </dict>
    <key>StandardOutPath</key>
    <string>/Users/marcus/tmp/launchd_pix.out</string>
    <key>StandardErrorPath</key>
    <string>/Users/marcus/tmp/launchd_pix.err</string>
</dict>
</plist>



You can adjust the max try count (maxtry) and retry interval (interval) in the shell script to suit your need.

Here {RunAtLoad = false} is omitted in plist because it is default.


Hope this may help,

H

Jun 4, 2015 2:23 PM in response to wondermac

I have almost the same problem

I have RunAtLoad=false in my plist.

And in the same time I have KeepAlive with PathState subkey.


My daemon always start with KeepAlive option. Event if RunAtLoad=false.


I tried to add AfterInitialDemand key.

But still no positive result.


Does anybody have the same problem?

I tried to implement the solution from the answer. But anyway - the same result.

Nov 1, 2016 9:48 AM in response to twtwtw

Thanks twtwtw


AfterInitialDemand was what I was looking for as well, not sure why its not documented in the man pages. The LaunchAgent now only runs on the Schedule time (once a week), and will continue to run every 'x' hours, if the job was missed due to not having VPN access or not being connected to the local LAN which validates connectivity before launching a backup script.


The only thing I was missing is not sending an exit code when the script was completed successfully. This was causing the script to be re-run until it exited with a 0.

launchd ignores RunAtLoad=false

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.