🐞 Cron Job Debug: SCP Failed Due to macOS Sleep Mid-Transfer
Adam C. |

Photo by Makoto Tsuka on Unsplash

Problem

We noticed that our scheduled cron/launchd job — responsible for restoring backups via scp — was intermittently failing. Logs showed:

SCP failed

Upon closer inspection, the failure was not due to permissions or file corruption, but rather that only a partial set of files (e.g., 106 out of 207) were transferred before the connection dropped.

Root Cause

The issue was traced back to macOS going to sleep during the job. Since the job ran early in the morning with the lid closed, the system entered sleep mid-transfer — interrupting the active scp session.

Solution

We wrapped the job with caffeinate to prevent sleep during execution:

<key>ProgramArguments</key>
<array>
  <string>/usr/bin/caffeinate</string>
  <string>-s</string>
  <string>/Users/adamcai/Projects/ss-db/restore.sh</string>
</array>

The -s flag keeps the system (not just the display) awake while the script runs. This ensures that scp completes without being cut off by sleep.

Result

After applying this fix, the backup restore job ran reliably every morning — with all files successfully transferred.