4

I'm trying to get a python script to run on startup. I have the following file: com.test.service.plist :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.test.service</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/sh</string>
        <string>/var/www/html/app/appstart.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

copied to ~/Library/LaunchAgents/

loaded as

launchctl load -w ~/Library/LaunchAgents/com.test.service.plist

the script has 2 lines:

/usr/bin/python /var/www/html/app/app.py
echo "hello" >> /var/www/html/app/test.txt

the script runs (the file test.txt gets created with 'hello' in it) however, the app.py doesn't run. for testing purposes, all i did inside the app.py was:

import os
os.system("echo 'python' >> /var/www/html/app/test.txt")

if i just run the appstart.sh in terminal, then python runs no problem

i followed instructions in this question already, but no luck

2
  • Why does your app.py script do the same thing as the bash script? Commented Apr 1, 2015 at 2:26
  • because I don't want to paste 200 lines of code in here and this is a very quick & easy way to test if the script runs. Commented Apr 1, 2016 at 19:13

2 Answers 2

1
+50

Some time ago I used cron to do just that. You can make an entry like this

@reboot /path/to/my/script

more info about cron

The path to my script will be the path to your appstart.sh file.

So you open your terminal.

You type in crontab -e (this will open a file in your default editor, problably nano)

You scroll down and on the bottom of the file you type @reboot /path/to/file

Then you save and exit.

Sign up to request clarification or add additional context in comments.

1 Comment

i know on apple site they advise against using cron, but it seems to be the only reliable task scheduler on OS X. Thanks for the answer.
1

Make sure the .plist file is chmod 644.

You asked for "run on Mac boot" and "run on startup" which tells me you are in the wrong directory. LaunchAgents is for running scripts on user login. To run on startup the .plist file needs to be in /Library/LaunchDaemons and owned by root:wheel.

If you don't need a shell to launch a shell script to launch python then I recommend a shortcut:

<key>ProgramArguments</key>
<array>
    <string>/usr/bin/python</string>
    <string>/var/www/html/app/app.py</string>
</array>

1 Comment

in my case it doesn't matter actually, running on user login is fine too, but neither of those worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.