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 !

  • 1
    Why not just click on Apple menu, System Preferences, Users and select your username and add your script to Login items? – Mark Setchell May 09 '15 at 21:29
  • I basically want to write a shell script to achieve this ! –  May 09 '15 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"}'` – Mark Setchell May 09 '15 at 21:38
  • Thanks ! it works but the script opens up in a text editor inspite of making it executable . –  May 09 '15 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'` – Mark Setchell May 09 '15 at 21:49
  • Yes, i did add a shebang . also the script works. but it also opens in the text editor. weird thing . –  May 09 '15 at 21:55
  • http://stackoverflow.com/questions/5125907/how-to-run-a-shell-script-in-os-x-by-double-clicking – Mark Setchell May 09 '15 at 21:58

1 Answers1

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.

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
  • It gives the following error when launchctl command is executed ! `Path had bad ownership/permissions` –  May 09 '15 at 21:04
  • @GreyBeard, ah, then your `.plist` isn't owned by `root:wheel`. Try `sudo chown root:wheel /Library/LaunchAgents/com.username.scriptname.plist` then. – Johannes Weiss May 09 '15 at 21:59
  • 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. – Johannes Weiss May 10 '15 at 11:46