Solved

Run Python script on startup

Posted on 2011-02-18
10
2,206 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
Want to upgrade to El Capitan instead of Sierra 10 286
How to scan rdp  ''only'' open port 3333? 5 151
Mac neophyte...printer issue... 13 113
yara deployment 8 103
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…
There is a security feature on iOS devices that is nearly impenetrable when it has been activated.  This article will provide some possible solutions as well as necessary steps to take to ensure you do not end up with a locked device.
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 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…

740 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