Sign up ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

This question has been asked quite a few times before but I didn't seem to get it working using the existing information.

My Pi runs Raspbian. I have a Python script named which loops forever.

I need it to run at boot. I know I have to create a .sh file containing something like

sudo python &

What I don't know is where this file should be or if it should contain anything else. I know about the init.d folder, but seeing the skeleton example I imagine there should be a simpler way to do this simple task.

share|improve this question

5 Answers 5

up vote 7 down vote accepted

If you want to control the process with commands like start, stop, restart etc using the skelton script and altering it for your purposes might be the best option.

If you just want the process to start and that's it, you might consider just putting the command, as you wrote it in your question, into the /etc/rc.local. (I don't have my RPi at hand, but I read online that there is an 'exit 0' line in there, you should put your command above this line)

share|improve this answer
It worked! I must add that setting the permissions for the script and rc.local back to 755 (read/write/execute) is a must. Not sure if both need this setting, but it worked for me. Thanks a lot for the help! –  Vlad Schnakovszki Dec 27 '12 at 0:07
"as you wrote it in your question" Not quite -- you don't need sudo as rc.local runs root. You should also specify the complete path to the script, obviously. You should also add & at the end so that the script forks, e.g. /path/to/ &. –  goldilocks Feb 20 at 13:15

Move your script (we will save it to the file dnscheck) to /etc/init.d/, and set the permissions so it can be run:

chmod 755 /etc/init.d/dnscheck

Add LSB init tags to the top of your script. You will probably want to change Required-Start/Stop and the Description Tags to fit your script.

# Provides:          dnscheck
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.

Then create the symbolic links by running

update-rc.d /etc/init.d/dnscheck defaults
share|improve this answer

To use a .py file, just put the line #!/usr/bin/python at the very start of your file. Then make it executable with chmod +x filename. Next, add the line: /path/to/ & to /etc/rc.local, with sudo privileges, swapping /path/to/ with the path to your script. This will execute at the end of boot.

share|improve this answer

Here is the solution that I constantly use.

Create a desktop file


type the following into it

[Desktop Entry]
Name=<Application Name Goes here>
Exec=  python /home/pi/Desktop/

paste this file into the


and restart your raspberry pi and it should automatically run your program in a new terminal

share|improve this answer
please don't cut and paste answers to multiple questions. If the answer is the same the newer version should be flagged as a duplicate. –  Steve Robillard Aug 25 at 19:29
The only reason I didnt do it was cause this page had a larger number of view as opposed to the other one. –  evolutionizer Aug 25 at 19:40
@SteveRobillard I also dont think I have the rep required to do so –  evolutionizer Aug 25 at 19:46
Flagging a post only takes 15 rep. Deciding what to do about it is the job of the moderators - so the number of views is irrelevant. Duplicate answers are automatically flagged by the system. They are a form of gaming the system. Therefore, I deleted the third one. –  Steve Robillard Aug 25 at 19:50
@SteveRobillard Thanks for the information and forgive my ignorance. I have there for flagged the previous question as duplicate. –  evolutionizer Aug 25 at 19:53

There are many ways to do this, of course, but don't forget using cron. If you put a @reboot line in your crontab, that command will be executed on every restart.

To test, I just added the following line to my user crontab with crontab -e: @reboot echo "$(date)" >> ~/boot.txt The bonus to this method is that you can call the job as required at other intervals besides just boot time, and you don't have to edit init scripts.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.