在Mac启动时运行python脚本 [英] running a python script on Mac boot

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

问题描述

我正在尝试让python脚本在启动时运行.我有以下文件:com.test.service.plist:

I'm trying to get a python script to run on startup. I have the following file: com.test.service.plist :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.test.service</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/sh</string>
        <string>/var/www/html/app/appstart.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

复制到〜/Library/LaunchAgents/

copied to ~/Library/LaunchAgents/

加载为

launchctl load -w ~/Library/LaunchAgents/com.test.service.plist

脚本有两行:

/usr/bin/python /var/www/html/app/app.py
echo "hello" >> /var/www/html/app/test.txt

脚本运行(文件test.txt用"hello"创建)但是,app.py无法运行.出于测试目的,我在app.py中所做的全部是:

the script runs (the file test.txt gets created with 'hello' in it) however, the app.py doesn't run. for testing purposes, all i did inside the app.py was:

import os
os.system("echo 'python' >> /var/www/html/app/test.txt")

如果我只是在终端中运行appstart.sh,那么python不会出现问题

if i just run the appstart.sh in terminal, then python runs no problem

我遵循了此问题中的说明,但没有运气

推荐答案

前段时间,我使用cron做到了这一点.您可以输入这样的内容

Some time ago I used cron to do just that. You can make an entry like this

@reboot/path/to/my/script

有关cron的详细信息

我的脚本的路径将是您的appstart.sh文件的路径.

The path to my script will be the path to your appstart.sh file.

因此,您打开终端.

您键入 crontab -e (这将在默认编辑器中打开一个文件,可能是nano)

You type in crontab -e (this will open a file in your default editor, problably nano)

您向下滚动并在文件的底部键入 @reboot/path/to/file

You scroll down and on the bottom of the file you type @reboot /path/to/file

然后保存并退出.

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

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