简体   繁体   中英

How can I run a Python script on Startup (macOS)?

I've created a Python script to organize my folders to my liking. It runs in the background, and rather than starting it up every time I boot my computer, I wanted it to automatically get ran on start up.

The method I am trying, is using the Automator application built into Mac. I've created an Application in Automator with the following code:

#!/bin/bash 
source ~/.bash_profile

python3 /Users/***/Documents/Programming/Python/DownloadsOrganizer/downloads-organizer.py

Essentially, it's just a script that loads my Python environment and then runs my Python program.

The script works fine in Automator if I click "Run", and it also works fine just running it normally in terminal. The issue comes in when I save the Application and add it to my Login Items under Users & Groups in System Preferences . It just doesn't work on startup.

I've done some digging, and if I right-click my application created through Automator, and click Show Package Contents then do Contents->MacOS I see an executable called Automator Application Stub . I believe this is what gets called on Startup. When I double click it, it opens a terminal window that says this:

/Users/***/Documents/Programming/Python/DownloadsOrganizer/DownloadsOrganizer.app/Contents/MacOS/Automator\ Application\ Stub ; exit;
-bash: /Users/***/Documents/Programming/Python/DownloadsOrganizer/DownloadsOrganizer.app/Contents/MacOS/Automator Application Stub: Bad CPU type in executable
logout
Saving session...completed.

[Process completed]

I've read online that it may be trying to load a 32-bit Python executable or something along those lines which was dropped in Catalina, but I'm not sure?

I'm just not sure how to fix this? If anyone has the solution or knows of a way to load a Python script on startup in a different manner, that would be appreciated.

Thank you.

Try running this through launchd rather than as an app in the startup items. To do that, you'll want to open a document in a plain text editor like BBEdit or TextEdit (make sure the latter is not in 'rich text' mode), then copy in the following plist:

 <?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>user.startup.organizer</string> <key>RunAtLoad</key> <true/> <key>ProgramArguments</key> <array> <string>/Library/Frameworks/Python.framework/Versions/3.7/bin/python3</string> <string>/Users/***/Documents/Programming/Python/DownloadsOrganizer/downloads-organizer.py</string> </array> </dict> </plist>

The string value for the Label key can be whatever you like; I just chose a likely name. The first string in the ProgramArguments array should be the full path to the python3 executable, which you can discover by running which python3 in Terminal.app.

Save this file as 'user.startup.organizer.plist' — the convention is to name the plist file with the value of the Label key — and put it in ~/Library/LaunchAgents/ . This should automatically run the python script when the user logs in. You can test it in Terminal.app by running the following commands:

  • launchctl load ~/Library/LaunchAgents/user.startup.organizer.plist : loads and runs the launchd job
  • launchctl remove user.startup.organizer.plist : removes the job so that you can test it again

I'm working on the assumption that the only reason you need the source command is to load the PATH variable for your normal interactive shell. If you get any errors, we may need to do a bit more, so let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM