Reputation: 1
I am new at this, so I haven't been able to make heads or tails when reading other posts that are similar questions.
I am trying to run a terminal command on startup, then close the terminal.
What I'm trying to run: cd /Applications/Sick-Beard-development [enter] python sickbeard.py -d [enter]
I am open to other solutions
Upvotes: 0
Views: 4462
Reputation: 365677
The easy way to do this is to get all the unnecessary stuff out of the way.
What you want to do is run a Python script at startup. You don't want to see it in a terminal window or anything, so there's no need to get Terminal.app involved. The only reason you've got a shell script involved is to set the working directory, and there are better ways to do that. And presumably the only reason you've dragged in AppleScript was for Terminal.
What you want to do is run your Python script as a launch agent. Apple's documentation on this may be a bit confusing for a novice, but there are some great blog posts around, like this one by Nathan Grigg.
The idea is that you create a plist file (either by editing the XML format by hand, or by using one of the plist editors that come with OS X and/or Xcode, or by using a third-party tool like Lingon) that tells OS X "when these conditions are true, run this command, in that working directory". Then you put that file in ~/Library/LaunchAgents. Then you tell launchctl
to load it. And you're done.
Your plist will look something like this:
<?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>org.user3159170.sickbeard</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python</string>
<string>/Applications/Sick-Beard-development/sickbeard.py</string>
<string>-d</string>
</array>
<key>WorkingDirectory</key>
<string>/Applications/Sick-Beard-development/</string>
<key>RunAtLoad</key>
<true />
<key>LaunchOnlyOnce</key>
<true />
</dict>
</plist>
These settings will make sure that OS X runs the program when you first load the Launch Agent, and again the first time you login after each subsequent reboot, and no other times, which I think is what you want. But man launchd.plist
will give you the details of all the options (or, again, read the linked blog post and search for others).
Save this as ~/Library/LaunchAgents/org.user3159170.sickbeard.plist.
Now, at the terminal, do this:
launchctl load ~/Library/LaunchAgents/org.user3159170.sickbeard.plist
You may want to look at your Console.log to see what happens. You can try starting and stopping it manually (man launchctl
will explain how, but basically it's just launchctl start org.user3159170.sickbeard
). Run launchctl list
to make sure it's showing up. Once it looks like it's all working, reboot your machine, log in, and make sure it ran again. If it did, you're done.
Upvotes: 4