Solved

Run Python script on startup

Posted on 2011-02-18
10
2,218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Create a default user profile for Mac OS X 10.7/10.8 Create a user account on OS X that will be a template for every other user of that computer. I usually call it “profile” and make it an administrator account for the time being. 1. Install a…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…
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…

752 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