Solved

Run Python script on startup

Posted on 2011-02-18
10
2,154 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now