Fezziwig
Posts: 2
Joined: Tue Jun 19, 2012 7:11 pm

Run python script at startup

Fri Jan 04, 2013 12:06 am

Although the title says python this isn't strictly about python so I though it might belong in this catagory.

I've followed many tutorials on how to do this now but none seemed to have done anything.
The way I'm currently trying is having a script in /etc/init.d/test

The script in that is as follows:

Code: Select all

#! /bin/sh
# /etc/init.d/test 

### BEGIN INIT INFO
# Provides:          noip
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Simple script to start a program at boot
# Description:       A simple script which will start / stop a program a boot /$
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Hello!"
    sleep 10s
    sudo python /home/pi/Desktop/Projects/test.py
    ;;
  stop)
    ;;
  *)
    echo "Bye!"
    sleep 10s
    killall python
    exit 1
    ;;
esac

exit 0
I put in the sleep commands so that I could definitely be sure it had run.
When I run the command:
sudo /etc/init.d/test start
Everything runs as expected with the 10 second pause and then it runs the python script.
When I reboot it just runs though all the normal lines of code(or what ever all the writing is) and goes to the desktop. Why might the script not be running?

User avatar
azeam
Posts: 194
Joined: Fri Oct 26, 2012 11:13 pm
Contact: Website

Re: Run python script at startup

Fri Jan 04, 2013 12:32 am

Have you done this:

Code: Select all

sudo update-rc.d test defaults
You should also skip the sudo since that script will be run by root during boot

User avatar
LetHopeItsSnowing
Posts: 350
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Run python script at startup

Fri Jan 04, 2013 8:29 am

Make sure you have run update-rc.d, its this that registers your script to run at boot and shutdown.
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

Fezziwig
Posts: 2
Joined: Tue Jun 19, 2012 7:11 pm

Re: Run python script at startup

Fri Jan 04, 2013 10:47 pm

I had done that, but it wasn't the problem. It turns out because in raspi-config I had set it up to go straight to desktop on boot, it skipped the script. Once I disabled that feature it worked perfect!

Thanks, for your replies.

nsweeting
Posts: 10
Joined: Sun Dec 23, 2012 3:56 pm

Re: Run python script at startup

Sun Jan 06, 2013 6:49 pm

Anyone know how to run a python script BEFORE the raspi-config auto login/boot? Having trouble figuring that out.

dwelch67
Posts: 819
Joined: Sat May 26, 2012 5:32 pm

Re: Run python script at startup

Sun Jan 06, 2013 11:56 pm

Is this thread related to bare metal programming?

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Run python script at startup

Mon Jan 07, 2013 2:17 pm

...or any programming at all? Maybe 'Using... -> Power Users' or 'Operating System... -> <which ever OS you are on>'??
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Beginners”

Who is online

Users browsing this forum: boyoh and 54 guests