0

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.

  • Some things I've tried so far are:

  • Going through the run permissions of each file No Change

  • Running both in and out of sudo Issue when run in sudo, as I'm in the Launch Agents, not Daemons

  • Using Launch Daemons: Same error

  • Implementing any all PATH variables from the terminal explicitly into the daemon: No Change

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>
2
  • 1. Where are you encountering this error 126? The launchctl command itself? Logged by the script? 2. It won't fix your immediate issue, but note that launching on com.apple.iokit.matching events requires the launched program to check and handle XPC events using xpc_set_event_stream_handler() or it will keep re-running your agent/daemon when it exits. I don't think it's possible to do this from a shell script, so you will need to find a different solution. Commented Mar 16, 2024 at 0:45
  • I get the error 126 from LaunchControl, which was recommended on a similar launchctl issue page. The error I get when trying to load the file through launchctl is ""Load failed: 5: Input/output error"" Commented Mar 16, 2024 at 20:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.