GascanTX
GascanTX

Reputation: 11

MacOS + launchd + python + mariadb = server connection fail

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:

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?

Upvotes: 1

Views: 37

Answers (0)

Related Questions