1

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 !

7
  • 1
    Why not just click on Apple menu, System Preferences, Users and select your username and add your script to Login items? Commented May 9, 2015 at 21:29
  • I basically want to write a shell script to achieve this ! Commented May 9, 2015 at 21:31
  • 1
    Just use something similar to this... osascript -e 'tell Application "System Events" to make login item at end with properties {path:"/Users/FreddyFrog/SomethingPythony"}' Commented May 9, 2015 at 21:38
  • Thanks ! it works but the script opens up in a text editor inspite of making it executable . Commented May 9, 2015 at 21:46
  • 1
    Has the script got a shebang as the first line with the path to the Python interpreter? And can you show the script? If not, make a really simple one and experiment with that - for example just have it create an empty file or directory in your Desktop so you can see if it ran, or make it do osascript -e 'beep 3' Commented May 9, 2015 at 21:49

1 Answer 1

2

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 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.

Sign up to request clarification or add additional context in comments.

3 Comments

It gives the following error when launchctl command is executed ! Path had bad ownership/permissions
@GreyBeard, ah, then your .plist isn't owned by root:wheel. Try sudo chown root:wheel /Library/LaunchAgents/com.username.scriptname.plist then.
Why is this downvoted even though it contains the correct answer? There has been another answer added 12h after this answer contains the same! Please note that the question was changed as a result of this answer and the final step (chowning the plist) is noted in the comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.