Azal-Amer
Azal-Amer

Reputation: 11

Macos:Launch Daemon LaunchCTL Error 129: Running a service when a USB is connected

I am currently trying to implement a launch daemon, such that when a USB device is connected, a shell script is ran. When I try to register the following daemon using launchctl load com.myplist.plist , I get an Error 126. I've tried using LaunchControl to identify the source of the bug, but all it is able to tell me is that the issue is with the shell.

Here's what the config looks like:

<?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.example.program</string>
    <key>ProgramArguments</key>
    <array>
    <string>/DIRECTORY TO FILE/testing.sh</string>
    </array>
    <key>LaunchEvents</key>
    <dict>
            <key>com.apple.iokit.matching</key>
            <dict>
                    <key>com.apple.device-attach</key>
                    <dict>
                            <key>idProduct</key>
                            <integer>1234</integer>
                            <key>idVendor</key>
                            <integer>1337</integer>
                            <key>IOProviderClass</key>
                            <string>IOUSBDevice</string>
                            <key>IOMatchLaunchStream</key>
                            <true/>
                    </dict>
            </dict>
    </dict>
</dict>
</plist>

And Here's what the shell looks like:

#!/bin/bash
# osascript -e 'display notification "" with title "RUNNING"'
USB_FILE="/Volumes/Kindle/documents/My Clippings.txt"
DEST_DIR="$HOME/Desktop"
PYTHON_SCRIPT="PATH TO FILE/KindleObsidianV2.py"
LOG_FILE="$HOME/Desktop/python_script.log"

if diskutil list | grep -q "Kindle"; then
    if [ $? -eq 0 ]; then
        rsync -av "$USB_FILE" "$DEST_DIR"
        osascript -e 'display notification "File copied successfully" with title "USB File Copied"'
        python3 "$PYTHON_SCRIPT"
    
    # Check if Python script had an error
        if [ $? -ne 0 ]; then
            # Display error notification
            osascript -e 'display notification "Python script encountered an error. Check log file." with title "Python Script Error"'
            # Save Python log to desktop
            mv "$LOG_FILE" "$HOME/Desktop/python_script_error.log"
        else
            # Delete copied file
            rm "$DEST_DIR/My Clippings.txt"
            # Display completion notification
            osascript -e 'display notification "Script execution completed successfully" with title "Script Execution Completed"'
        fi
    else
        osascript -e 'display notification "Failed to copy file from USB. Check USB connection and try again." with title "Rsync Error"'
    fi
else
    echo "USB device not found."
fi

The shell file itself runs as expected when run through terminal.

Any help or suggestions would be much appreciated!

EDIT: I encountered the same issue when trying to instead run the shell at a timed increment, with the below new 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>com.amer.kindle_monitor.plist</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>20</integer>
    <key>KeepAlive</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/amer_/Desktop/KindleProject/testing.sh</string>
    </array>
</dict>
</plist>

Upvotes: 0

Views: 38

Answers (0)

Related Questions