Solved

Run Python script on startup

Posted on 2011-02-18
10
2,178 Views
Last Modified: 2012-05-11
Hey,

I want to run a python script on startup in OS X, elegantly. At the moment, I use the code below which leaves an apple script icon which says "Application (not responding)"

Below is the code for the apple script and a screenshot.

Cheers. screenshot20110218at101.png
do shell script "python /users/ashsaba/sick-beard/sickbeard.py"

Open in new window

0
Comment
Question by:chipped
  • 5
  • 3
10 Comments
 

Author Comment

by:chipped
ID: 34924853
Oh yeah, I save the code as an Application, then add that to login items.
0
 
LVL 8

Expert Comment

by:et01267
ID: 34925398
This sounds like the python script is not exiting cleanly, and is "hanging" around.  It could also have something to do with the environment the script is executing within.  For example, Applescript always uses /bin/sh as the shell to run commands within, regardless of what your terminal shell may be.

I presume that you have run the script successfully from a terminal, and that your script application works correctly when you launch it manually?
0
 
LVL 8

Expert Comment

by:et01267
ID: 34925473
Oh, I guess Applescript is waiting for output from your shell command, and this never happens.

Perhaps putting a '&' at the end of the command will allow it to fork and the Applescript will then complete.  As in

do shell script "python /users/ashsaba/sick-beard/sickbeard.py &"
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Closing Comment

by:chipped
ID: 34927180
Thanks.

The python script is actually an application that is based on python, so it's always running.
0
 

Accepted Solution

by:
chipped earned 0 total points
ID: 34932695
Seems I spoke too soon. The above solution didn't work. I used launchd, by making a plist.
<?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.sickbeard.sickbeard</string>
  <key>ProgramArguments</key>
  <array>
       <string>/usr/bin/python</string>
       <string>/Path To Script/SickBeard.py</string>
       <string>-q</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

Open in new window

0
 
LVL 8

Expert Comment

by:et01267
ID: 34932940
Interesting.  What didn't work when you used the Applescript ?  
0
 
LVL 1

Expert Comment

by:rodaddy
ID: 34934220
There are a couple of ways that I have done this in the past.  I have noticed that if I wrap my python script in an .sh script with all the proper environments loaded that I rarely if ever have a problem with a hang'n app.

What I mean is:

create startSickBeard.sh

chmod 775 startSickBeard.sh

Then as et01267 says, run sickbeard.py with the '&' at the end, but put the full path to your proper python.  Just in case you have more then one installed python(s), bash might not start the same one you would normally start when your in command or have your normal environment set up.

/'pathToPython'/python /users/ashsaba/sick-beard/sickbeard.py &

That's all that needs to be in the startSickBeard.sh.  

I have had to do this when running somewhat disgruntled python scripts from CRON at startup as well.  
0
 

Assisted Solution

by:chipped
chipped earned 0 total points
ID: 34935218
Thanks for the suggestion, but I've already found a working solution, which seems better suited to what I need. I only need to run on startup, not off a schedule.

I forgot to mention, the plist should be saved as:

/Users/username/Library/LaunchAgents/com.sickbeard.sickbeard.plist

To start without restarting, use:

launchctl load ~/Library/LaunchAgents/com.sickbeard.sickbeard.plist
launchctl start com.sickbeard.sickbeard

And a link to the source, where I found the solution http://sickbeard.com/forums/viewtopic.php?f=3&t=87

I used MacVim to create the plist (TextEdt was being difficult) http://code.google.com/p/macvim/
0
 

Author Closing Comment

by:chipped
ID: 34968309
:)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Selenium Webdriver Login Issues On Mac 5 109
Finder searched too broad a disk region 8 64
How To Loop - Python 19 86
Downgrading from Python 3.4.5 to 3.4.3 5 38
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question