在Mac启动上运行python脚本

我正在尝试在启动时运行
python脚本.我有以下文件: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 /

加载为

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中所做的就是:

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

如果我只是在终端中运行appstart.sh,那么python运行没有问题

我已经遵循了this question的说明,但没有运气

最佳答案 前段时间我用cron来做到这一点.你可以这样做一个条目

@reboot / path / to / my / script

more info about cron

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

所以你打开你的终端.

你输入crontab -e(这将在你的默认编辑器中打开一个文件,problably nano)

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

然后保存并退出.

点赞