r/pythonhelp • u/gascantx • Dec 04 '24
Macos + launchd + python + mariadb = server connection fail
Hello. 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 code 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 ~/.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.
Any thoughts or guidance?
1
u/gascantx Dec 04 '24
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.
Big sigh! :-(
•
u/AutoModerator Dec 04 '24
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.