# Running launchd agent not preventing macbook from sleeping

I have an agent/daemon that runs a backup script (I use restic) at 4:05am on a StartCalendarInterval. My understanding is that if a launchd agent is running, that should prevent osx from sleeping. Is that wrong?

I can see that my script partially worked as a snapshot was created in the repository, but when I went to check the snapshot, the repo had a lock on it which indicates to me something happened mid-run.

Here is my plist file for the agent.

<?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>backup.restic.username</string>
<key>ProgramArguments</key>
<array>
<string>/Users/username/Documents/scripts/backup.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>5</integer>
<key>Hour</key>
<integer>4</integer>
</dict>
</dict>
</plist>


Is there something I can add there to prevent sleeping? I will say the macbook wasn't plugged in and was at 35% battery when I woke up.

• Most probably not related to the issue, but is it a copy/paste issue or does your plist really contain the first two lines twice? – nohillside Jan 6 '19 at 16:29
• As for the question as such: Check out man caffeinate and a bunch of questions/answers on this site regarding its use and constraints. – nohillside Jan 6 '19 at 16:31
• @nohillside, copy/paste issue. Sorry! A brief look and caffeinate appears to be what I need. Thanks! – user650271 Jan 6 '19 at 16:35

## 1 Answer

@nohillside gave me what to lookup. Updated to use caffeinate to prevent disk and system from sleeping.

<?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>backup.restic.username</string>
<key>Program</key>
<string>/usr/bin/caffeinate</string>
<key>ProgramArguments</key>
<array>
<string>-im</string>
<string>/Users/username/Documents/scripts/backup.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>5</integer>
<key>Hour</key>
<integer>4</integer>
</dict>
</dict>
</plist>

• When using ProgramArguments the first string is actually assigned to \$0 during execution (so should be the name of the program again). – nohillside Jan 6 '19 at 19:12
• I've tested this a few times with launchctl start backup.restic.username and it works as intended. I don't see any errors in /var/log/system.log as well. Could it be a change in launchd versions? Based on my googling, they've changed a lot in recent years :D. Edit: It looks like you are correct UNLESS you also use the key Program. – user650271 Jan 6 '19 at 20:44