Context
There is a similar question here, however I feel the answer does not expand on how to "invoke the user environment profile from the launchd script"; and the alternative solution of identifying differences in the user and launchd environments shouldn't apply since I am running a launchd agent (run within user context) and not a launchd daemon (run within system context).
My setup
I have a run_rsync_back.sh
file in my home directory with the following (note that this script runs sucessfully when manually run from the terminal):
date >> ~/.cron.log && rsync --stats -hai --delete /Users/username/Documents/ [email protected]:/home/username/Documents_MacbookPro/ >> ~/.backup.log 2>&1
I have ensured the above file is executable with chmod +x
And created a launchd Property List file for my agent:
~/Library/LaunchAgents/com.rsyncbackup.plist
with the following:
<?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.rsyncbackup</string>
<key>ProgramArguments</key>
<array>
<string>/Users/username/.run_rsync_backup.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>7200</integer>
</dict>
</plist>
After starting the daemon (launchctl load ~/Library/LaunchAgents/com.rsyncbackup.plist
), I can confirm that nothing was transferred to the server, and the following error appears in the ~/.backup.log
file:
Sat Aug 29 17:19:16 CEST 2020
rsync: [sender] opendir "/Users/username/Documents/." failed: Operation not permitted (1)
IO error encountered -- skipping file deletion
Number of files: 1 (dir: 1)
Number of created files: 0
Number of deleted files: 0
Number of regular files transferred: 0
Total file size: 0 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 0
File list generation time: 0.015 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 67
Total bytes received: 70
sent 67 bytes received 70 bytes 274.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1307) [sender=3.2.2]
I have also tried the above on an out-of-the-box Mac, with exactly the same result!