Running launchd agent not preventing macbook from sleeping

launchd

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.

Best 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>