1

I am new to this group. Hopefully someone can provide some guidance to solve my issue... I am attempting to schedule the running of my python program on my Mac using launchd.

I have hit a roadblock using launchd to periodically start a python script that collects some data from the mac locally (file based data), then connect to a remote mariadb server and insert the data to the appropriate tables. When I run the python program manually (without launchd), it works perfectly. When I run the python program with launchd, it runs creates my log file, imports the appropriate packages, etc. When it attempts to connect to the remote db server, it fails.

2024-12-04 08:55:00 -- PROCESS START - connecting to database
2024-12-04 08:55:00 -- Error: Can't connect to server on '192.168.1.3' (65)
2024-12-04 08:55:00 -- PROCESS END - terminating

The error above comes from the python code:

try:
    conn = mariadb.connect(
        user="user",
        password="password",
        host="192.168.1.3",
        port=3306,
        database="my_database"
    )

except mariadb.Error as e:
    print(f"Error: {e}")
    errorText = f"Error: {e}"
    log_write(errorText)

My launchd was configured using the following 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.ccg.launchphotofileminer</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/ccg/MyLaunchAgents/launch-photo-miner</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

 <key>StartCalendarInterval</key>
 <dict>
   <key>Minute</key>
   <integer>55</integer>
 </dict>

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

  <key>WorkingDirectory</key>
  <string>/Users/ccg/MyLaunchAgents</string>

  <key>StandardErrorPath</key>
  <string>/Users/ccg/MyLaunchAgents/photofileminer.err</string>

  <key>StandardOutPath</key>
  <string>/Users/ccg/MyLaunchAgents/photofileminer.out</string>
</dict>
</plist>

The plist calls a bash script which sets up the python environment and then launches the python code:

source /Users/ccg/.venv/bin/activate
cd /Users/ccg/MyLaunchAgents
/Users/ccg/.venv/bin/python3  > /Users/ccg/MyLaunchAgents/photo.log 2>&1photo-file-miner.py

System details:

  • Intel based Mac running 15.1.1
  • Python 3.12 installed via BREW
  • Mariadb connector installed via PIP3

I have used the same bash script as a launcher for cron in place of launchd and I get the exact same errors.

So I changed the IP address in my python code from "192.168.1.3" to 192.168.1.2" which is an unused address on my local network. I get the exact same error. I tried another address which is used but is definitely not running MariaDB and I again get the exact same error. So I thought this meant that I don't have network connectivity when run via launchd. So... I inserted following line in to my bash launch script to see if simple bash could reach the remote server/port:

nc -v  3306192.168.1.3

When I run this locally and in the bash launch script via launchd, I can read from the MariaDB port. So... I have connectivity. I suspect there is something odd about the python code and the mariadb connect library.

Any thoughts or guidance?

5
  • Maybe the network is not yet up at that stage of the boot process? I would recommend to do a wait loop with 1. connect 2. wait 10 seconds 3. retry 4. repeat N times to see if this helps. Commented Dec 4, 2024 at 18:45
  • 1
    Thanks Mikko. My launchd agent is not running upon boot. It is on a timer to run at a specific minute every hour. The network is definitely up. Commented Dec 4, 2024 at 18:50
  • Ok, more debug accomplished. I replicated a version of this setup to Linux running in a VM on my Mac. Instead of launchd I used cron and all worked in the VM with zero issues. So this issue is definitely a Mac issue since this scenario fails with cron and launchd natively on the Mac. I am at a loss on how to fix this. Any advice is appreciated. Commented Dec 4, 2024 at 20:51
  • Errno 65 = EHOSTUNREACH (No route to host) Commented Dec 5, 2024 at 3:55
  • Right.. Just don't understand why. Its like there is a firewall or something preventing the traffic when run via launchd or cron. I definitely have the OS based firewall turn off. Funky. Commented Dec 5, 2024 at 13:01

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.