繁体   English   中英

在OSX启动时运行Python脚本

[英]Running Python Script at startup of OSX

我有一个Python脚本,我想在登录Mac时立即执行。 我在互联网上尝试了各种方法。 但是它们似乎都不起作用。

我尝试将com.username.scriptname.plist文件放置在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>com.username.scriptname</string>

  <key>Program</key>
  <string>/Users/username/scriptlocation/scriptname.py</string>

  <key>RunAtLoad</key>
  <true/>

  <key>StandardErrorPath</key>
  <string>/tmp/com.username.scriptname.err</string>

  <key>StandardOutPath</key>
  <string>/tmp/com.username.scriptname.out</string>
</dict>
</plist>

我将脚本放置在.plist文件中提到的位置,并运行以下launchctl load /Library/LaunchAgents/com.username.scriptname.plist 但是似乎什么也没发生。 我错过了任何步骤还是做错了什么? 我需要更改任何设置吗?

错误:

->grep com.username.scriptname /var/log/syslog
grep: /var/log/syslog: No such file or directory

->launchctl list com.username.scriptname
{
    "StandardOutPath" = "/tmp/com.username.scriptname.out";
    "LimitLoadToSessionType" = "Aqua";
    "StandardErrorPath" = "/tmp/com.username.scriptname.err";
    "Label" = "com.username.scriptname";
    "TimeOut" = 30;
    "OnDemand" = true;
    "LastExitStatus" = 19968;
    "Program" = "/Users/username/scriptname.sh";
};

当我有一个Python文件时,它如何显示scriptname.sh很奇怪!

.plist看起来正确,因此肯定有问题(我的猜测:无法执行或脚本路径错误)。

您可以做什么调试:

  • 检查syslog中有关您的工作的内容: grep com.username.scriptname /var/log/syslog 可能类似于com.apple.xpc.launchd[1] (com.username.scriptname[PID]): Could not find and/or execute program specified by service: 13: Permission denied: /Users/username/scriptlocation/scriptname.py
  • 检查发布的内容与您的工作有关: launchctl list com.username.scriptname
  • 另外: launchctl list | grep com.username.scriptname什么? launchctl list | grep com.username.scriptname说什么?

还请注意man launchd.plist必须说什么:

RunAtLoad <boolean>此可选键用于控制在加载作业时是否启动作业。 默认为false。 应避免使用此密钥 ,因为投机性作业启动会对系统引导和用户登录方案产生不利影响。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM