i am trying to create a service for mac that triggers a python file whenever a usb is inserted or ejected in the system. My code works for this functionality but when i shut down my pc and restart the device the service randomly triggers the python script even when i have not inserted any usb.
launchctl_daemon_usb_trigger(){
plist_name=$1
plist_path="$plist_name.plist"
plist_filename=$(basename "$plist_path")
install_path="/Library/LaunchDaemons/$plist_filename"
trigger_name=$2
# echo "installing launchctl plist: $plist_path --> $install_path"
log_message "Service Name <'$plist_name'>"
log_message "Install Path <'$install_path'>"
log_message "Setting up trigger event named <'$trigger_name'>."
log_message "Creating the launchd service <'$plist_path'>."
log_message "Executing command: <$PYTHON3_DIRECTORY $ETC_PYTHON_ENV $trigger_name>"
# Set the content of the plist file
cat <<EOF | sudo tee "$install_path" > /dev/null
<?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>$plist_name</string>
<key>ProgramArguments</key>
<array>
<string>$PYTHON3_DIRECTORY</string>
<string>$ETC_PYTHON_ENV</string>
<string>$trigger_name</string>
<string>$ORGANIZATION_NAME</string>
<string>$DEPARTMENT_NAME</string>
<string>$SECTION_NAME</string>
</array>
<key>WorkingDirectory</key>
<string>$DESTINATION_WORKING_DIRECTORY</string>
<key>RunAtLoad</key>
<false/>
<key>WatchPaths</key>
<array>
<string>/Volumes</string>
</array>
<key>StandardOutPath</key>
<string>$LOG_SERVICE_DIRECTORY/$plist_name.out</string>
<key>StandardErrorPath</key>
<string>$LOG_SERVICE_DIRECTORY/$plist_name.err</string>
</dict>
</plist>
EOF
#giving the nescessary permissions
sudo chown root "$install_path"
sudo chmod 644 "$install_path"
#unload the service first
# echo "unloading the service"
sudo launchctl unload "$install_path" > /dev/null 2>&1
#load the service first
# echo "loading the service"
sudo launchctl load "$install_path" > /dev/null 2>&1
log_message "Done creating the launchd service <'$plist_path'>."
}