Solved

Run Python script on startup

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

A professional opinion on which Apple product to buy, and a tidbit about the WWDC.
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

624 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