LaunchDaemon File Doesn’t Work – Troubleshooting Guide

automationlaunchdplist

I have a .plist file in the LaunchDeamons directory. It's supposed to run a .sh file (which works) which creates a backup of a directory onto an external drive every day at 00:00. I have tried putting this plist file into LaunchAgents and it still won't work.
This is the .plist file:

<?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>com.postgres.backup.0000</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/tristan/Documents/Server_Backup/Backup.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>0</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

I ran these commands which I got from here:

sudo launchctl unload -w /Library/LaunchDaemons/com.postgres.backup.daily.0000.plist
sudo launchctl load -w /Library/LaunchDaemons/com.postgres.backup.daily.0000.plist
sudo launchctl start /Library/LaunchDaemons/com.postgres.backup.daily.0000.plist

But the error persists:

Bootstrap failed: 5: Input/output error

I ran chmod +x /Users/tristan/Documents/Server_Backup/Backup.sh so it should be executable.

Edit:

I added logging to the LaunchDeamon but the log files just stay empty. That would mean that the Deamon isn't even running in the first place. After trying to load and start it I still get the same error as before. This is the code for the logs which I put at the top:

<key>StandardOutPath</key>
<string>/Users/tristan/Server_Backup/daily.0000_out.log</string>
<key>StandardErrorPath</key>
<string>/Users/tristan/Server_Backup/daily.0000_error.log</string>

Edit 2:
I remade the .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>postgres.backup</string>
    <key>Program</key>
    <string>/Users/tristan/Server_Backup/Backup.sh</string>
    <key>StandardOutPath</key>
    <string>/Users/tristan/Server_Backup/daily.0000_out.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/tristan/Server_Backup/daily.0000_error.log</string>
    <key>UserName</key>
    <string>root</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>18</integer>
        <key>Minute</key>
        <integer>03</integer>
    </dict>
</dict>
</plist>

Now the Deamon does run and it creates logs but it still cant run the shell script (I'm guessing because of lack of permissions).

The sell script is:

#!/bin/bash

SOURCE_DIR="/Users/tristan/Library/Application Support/Postgres/var-16"
BACKUP_DIR="/Volumes/Malbec/Backup"

BACKUP_NAME="backup_$(date +%Y%m%d_%H%M%S)"
BACKUP_PATH="$BACKUP_DIR/$BACKUP_NAME"

rsync -a --delete "$SOURCE_DIR/" "$BACKUP_PATH/"

I get this error:

rsync: mkdir "/Volumes/Malbec/Backup/backup_20240430_180305" failed: Operation not permitted (1)
rsync error: error in file IO (code 11) at /AppleInternal/Library/BuildRoots/0032d1ee-80fd-11ee-8227-6aecfccc70fe/Library/Caches/com.apple.xbs/Sources/rsync/rsync/main.c(545) [receiver=2.6.9]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /AppleInternal/Library/BuildRoots/0032d1ee-80fd-11ee-8227-6aecfccc70fe/Library/Caches/com.apple.xbs/Sources/rsync/rsync/io.c(453) [sender=2.6.9]

Best Answer

When I got this error message: Bootstrap failed: 5: Input/output error The Deamon was already running. The thing that didn't let it run was this part of the code:

<key>RunAtLoad</key>
    <false/>

which doesn't let the deamon start when booting the computer (It has to be true). I have now put the file into the LaunchAgents directory and it works perfectly fine! The only problem is the one with the shell file but that doesn't work because of a different reason. (It works if it creates the folder onto the internal hard drive.)