Home » How Do I? » Auto Run On Power Up
  • Auto Run On Power Up

    One of our students, wanted to make a Twitter machine – but it has to be on all the time, so what happens if there is a power cut?

    How can I make a program start up without needing to type anything in the command line? I want to run a Python program with my Raspberry Pi, but I don’t want to have to plug in the keyboard to run it each time. How can I make sure that if there is a power-cut, my Raspberry Pi re-boots and starts my program again?  This is a simple way of starting the Python script when the Raspberry Pi powers on


    Open up a terminal window on the Raspberry Pi. This is often called the command line. This is a black background, with white writing (looks like DOS for old school users!)

    At the terminal, type the following command:

    sudo nano ~/.config/lxsession/LXDE-pi/autostart

    A file will then open for you to add code. Just add a single line at the bottom of the existing lines to automatically start your program on the Pi powering up using Python 3:

    sudo /usr/bin/python3 /home/pi/***FOLDER***/***yourfilename***.py
    Then when you are happy use Ctrl+X to exit and “Y” to save the changes.

    When you reboot your Raspberry Pi, the script should run. The next time you plug power in to your Pi, ***yourfilename***.py will be running in the background. You will not see any evidence unless you have some print commands or message windows in the script.

    Try it out and see if you can make a simple program start up. This is really good for a Twitter Bot, that scans Twitter for mentions and re-Tweets them on your own account. If the power goes, it works as soon as it boots up again. It can sit on the shelf without needing a screen, keyboard or mouse, just an internet connection.

    Tags: , , , , ,