在OS X启动时运行Python脚本 [英] Run Python Script at OS X Startup

查看:83
本文介绍了在OS X启动时运行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 python 和 MAC OSX 都很陌生.对于我的学术项目,我需要使用Twitter流API从Twitter下载一堆推文.我需要下载至少5000000条推文.因此,我编写了一个python脚本并将其放置在启动中.系统偏好设置 -> 用户和组 -> 登录项"并在那里添加了我的脚本.但是我看到在登录系统时脚本没有执行!请帮助我解决此问题.

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.

推荐答案

相应地调整以下内容,将其命名为 myscript_launcher.plist ,然后将其放在以下三个位置之一:/System/Library/LaunchAgents /System/Library/LaunchDaemons /Users/<用户名//Library/LaunchAgents .

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>

此外,我们假设您将plist文件放在〜/Library/LaunchAgents 中.您可以使用 launchctl 来启动和停止它.首先,使用 launchctl load〜/Library/LaunchAgents/myscript_launcher.plist .要停止它,请执行相同的操作,但使用 unload 参数

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

这篇关于在OS X启动时运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆