在Mac启动时运行python脚本

编程入门 行业动态 更新时间:2024-10-27 06:20:32
本文介绍了在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 www.apple/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脚本

本文发布于:2023-11-05 06:22:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   脚本   Mac   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!