简体   繁体   中英

Running Python Script at startup of OSX

I have a Python script that I want to execute as soon as I login into my Mac . I have tried various methods on the internet. But none of them seem to work .

I tried placing the com.username.scriptname.plist file in 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>

I placed my script in the location mentioned in the .plist file , and ran the following launchctl load /Library/LaunchAgents/com.username.scriptname.plist . However nothing seems to happen . Am I missing any step or doing anything wrong ? Do I need to change any setting ?

Error:

->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";
};

Its weird how it shows scriptname.sh when I have a Python file in place !

This .plist looks correct, so there must be something wrong (my guesses: not executable or wrong path to the script).

What you can do to debug:

  • Check what's in syslog about your job: grep com.username.scriptname /var/log/syslog . There might be something like 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
  • Check what launchd has to say about your job: launchctl list com.username.scriptname
  • Also: What does launchctl list | grep com.username.scriptname launchctl list | grep com.username.scriptname say?

Also mind what man launchd.plist has to say:

RunAtLoad <boolean> This optional key is used to control whether your job is launched once at the time the job is loaded. The default is false. This key should be avoided , as speculative job launches have an adverse effect on system-boot and user-login scenarios.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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