A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/29338066/mac-osx-execute-a-python-script-at-startup below:

macos - Run Python Script at OS X Startup

Asked 10 years, 4 months ago

Viewed 31k times

I am very new to python as well as MAC OSX. For my academic project I need to download a bunch of tweets from twitter using twitter streaming API. I need to download atleast 5000000 tweets. So I have written a python script and placed it in start-up. "System Preference -> Users and Groups -> Login items" and added my script there. But I see that the script is not executed when I login to the system ! Please help me resolve this issue.

Karl Amort

16.4k88 gold badges6868 silver badges7777 bronze badges

asked Mar 30, 2015 at 3:41

whitetigerwhitetiger

42211 gold badge55 silver badges1414 bronze badges

Adapt the following accordingly, name it something like myscript_launcher.plist, and put it in either one of three locations: /System/Library/LaunchAgents, /System/Library/LaunchDaemons, /Users/<username>/Library/LaunchAgents.

<?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>my.python.script.name</string>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/python</string>
        <string>/path/to/python/script.py</string>
    </array>
    <key>StandardErrorPath</key>
    <string>/var/log/python_script.error</string>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>

Also, let's assume you put the plist file in ~/Library/LaunchAgents. You can start and stop it with the launchctl. To start, use launchctl load ~/Library/LaunchAgents/myscript_launcher.plist. To stop it, do the same but use the unload argument

answered Mar 30, 2015 at 3:52

TommyTommy

62055 silver badges77 bronze badges

4

You need to create a shell file that launches your python code. Tell the shell script to start at log in.

example

#!/bin/bash
python username/Desktop/startupscripts/file.py

save the file with the .sh extension.

add the .sh file to "System Preference -> Users and Groups -> Login items", the .sh script will call the python file.

This is what you should have in the shell file. I'm assume this is the path to the file, if it isn't modify it.

#!/bin/bash
python /Users/username/moviebuzz-api/flask/bin/streaming_movies.py

nothing else.

answered Mar 30, 2015 at 3:52

reticentrootreticentroot

3,69222 gold badges2525 silver badges4242 bronze badges

14

But I see that the script is not executed when I login to the system !

After that, you can set the script as Login Items in the way you posted.

How to set Terminal.app as default:
  1. Put a #! line on the top of the script:
#!/usr/bin/env python3

print("hello world")
  1. Add execute permission to the script:
chmod u+x script.py
  1. Change the file extension .py to .command (to associate the file with Terminal.app):
mv script.py script.command

At this point, you should be able to run the script by double clicking on Finder.
If you can not, try the following:

  1. Right click on the script file on Finder.
  2. Select Get Info
  3. Click the dropdown of Open with: option.
  4. Select Terminal.app

By the way, the above applies to not only python, but other scripts such as bash.

For example of bash, change !# like so:

#!/usr/bin/env bash

answered Jul 8, 2022 at 9:43

catwithcatwith

1,3451515 silver badges2020 bronze badges

1

Here's the full solution I am using on Mojave:

Plist: ~/Library/LaunchAgents/play.with.mpv.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>Disabled</key>
    <false/>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/Users/e/Library/Python/3.7/bin:/usr/local/bin</string>
    </dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>play.with.mpv</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/python3</string>
        <string>/Users/e/Library/Python/3.7/bin/play-with-mpv</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/play.with.mpv.stderr</string>
    <key>StandardOutPath</key>
    <string>/tmp/play.with.mpv.stdout</string>
</dict>
</plist>

Mind the PATH to python3 - find it with which python3 and the play-with-mpv python script!

Load plist:

launchctl load ~/Library/LaunchAgents/play.with.mpv.plist

If your video is dropping frames as hell, create mpv config in ~/.config/mpv/mpv.conf and put there this:

video-sync=display-resample

if that do doesn't help, try this instead

opengl-early-flush=no

answered Jan 7, 2019 at 2:07

5ulo5ulo

79911 gold badge99 silver badges2121 bronze badges

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4