在启动时执行python脚本

在启动时执行python脚本

本文介绍了MAC OSX-在启动时执行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,.

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中.您可以使用initctl启动和停止它.要开始,请使用initctl 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 initctl. To start, use initctl load ~/Library/LaunchAgents/myscript_launcher.plist. To stop it, do the same but use the unload argument

这篇关于MAC OSX-在启动时执行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:25