Thanks for looking at this. Turns out that launchd couldn't find my user installed version of Python. I added the EnvironmentVariables key to the launchd .plist file and provided a $PATH variable. Doing this, the example code now logs my Python version and moving on to the actual script, then also worked. :)
Post
Replies
Boosts
Views
Activity
One more update. I have made a minimal demo which shows the issue.
#!/usr/bin/env python3
# trying: python --version
# in my shell gives this: Python 3.10.15
# /opt/pkg/bin/python3 fails exit(2)
# /opt/pkg/bin/python fails exit(2)
# /usr/bin/env python3 works exit(0), resolves to system supplied version! (3.6.9)
# /usr/bin/env python fails exit(127)
# /usr/bin/python3 works exit(0), system supplied version!
import sys
import os
log_path = "/tmp/test_log.txt" # Simple location
try:
with open(log_path, "a") as f:
f.write(str(sys.version))
f.flush()
print("Write successful")
except Exception as e:
print(f"Error writing to file: {e}")
I'm going to update the post with some additional information, not sure if I can actually edit the post.
The script itself is written in python and this script is launched by a shell_wrapper.sh which does exit with status 0. Turns out that if I instead launch the python script directly, it exits with status 127. So using the wrapper was just hiding the exit code.
I have the Xcode developer tools installed and the supplied system version of Python which I believe is 3.9. In addition to this I have also installed, Python version 3.10 with the pkgsrc package manager, to resolve which Python version to use, I do #!/usr/bin/env python, now this seems to have confused launchd, so I hardcoded the path to my version of Python like this: #!/opt/pkg/bin/python and now I get an exit code 2, when the script is launched by launchctl kickstart. I also have some python modules that I have installed like selectolax, maybe those can't be found by launchd, which may have different env variables set than my user environment.