Launching Python Script when Raspberry Pi Boots (Console & Desktop Start)

We have been thinking about using Raspberry Pi 3 as our computer to control all the test equipment at work.  One idea is to use Python to control our equipment (like FTDI devices mentioned in the previous post).   So this got me thinking about how I could get a Raspberry Pi to launch the program when it first boots up.  After reading a bunch of posts on the wild wild internet, I came up with some ways to do this.


Console Mode Python Scripts Only

First, we need to make sure that we start up in the console mode rather than the X-window because the python script wouldn’t be executed from the console in the GUI mode. You could do this either by setting the Start X-server after boot to disable via

$ sudo raspi-config

Choose option 3 (Boot Options) to choose the boot environment.  To boot to console and login automatically use option B2 (Console Autologin Text console, automatiaclly logged in as ‘pi’ user)

Second, we need to be able to launch a python script right when the Pi boots up.  This is easy enough to do.  You just have to modify the profile file on Pi

sudo nano /etc/profile

Scroll to the bottom of the file and then add a script that you want to launch. In my case, I want to launch a python script called startupScript.py from my /home/pi directory.

sudo python /home/pi/startupScript.py

Now to test this, we can create the startup script and see whether it gets executed at boot time. Create our startupScript.py by using nano.

$ sudo nano /home/pi/startupScript.py

Here’s just a simple python code to go inside startupScript.py to display text:

#!/usr/bin/python
print "**********************************************"
print "*  This is a start up test script inside     *"
print "* /home/pi/startupScript.py                  *"
print "* Other python script can run at start up    *"
print "* by editing /etc/profile                    *"
print "**********************************************"

Save the script and then initiate a reboot on the Raspberry Pi:

reboot

When Pi boots back up again you can see that our python script got executed and the text message got displayed right before the console.


Qt4 GUI Python Scripts

Now what if we want to run the scripts with GUI?  Obviously this is problematic if the GUI requires to have X-window running.  Qt4 script would need this so we need to start from the X-window mode.   We can re-enable this with raspi-config again or use the command:

$ sudo update-rc.d lightdm enable

This time choose the B4 option (Desktop Autologin Desktop GUI, automatically logged in as ‘pi’ user.  When being asked to reboot, say no for now so that we can add some startup scripts.

Remember to remove the python script being launched in /etc/profile since that’s still enabled.

To use PyQt4, we need to first install it in our Raspberry Pi. The -y parameter at the end of each command automatically answer yes to any prompt during install so we don’t have to type yes each time to continue the installation.

$sudo apt-get install python-qt4 -y
$sudo apt-get install pyqt4-dev-tools -y
$sudo apt-get install qtcreator

We should now have everything we need to make a python Qt4 program. Let’s create a generic Qt4 program that just shows a blank GUI window. I’ll call this render.py and I’ll save it in /home/pi folder.

sudo nano /home/pi/render.py

Here’s the code for render.pi:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui

def main():

    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

You can try running this script on the desktop and you should see a blank window showing up. Now let’s see if we can just launch this program when our GUI desktop first starts up.

To launch the script at startup, modify the /etc/xdg/lxsession/LXDE-pi/autostart would work.

First edit autostart file:

$ sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

Add the following line at the end to launch your PyQt4 script:

@sudo python /home/pi/render.py

Save the file and exit.  We need to make sure the file permission is correct so that it can auto-launch.

$ sudo chmod +x render.py

Now we can reboot and when the Desktop starts again, our render.py program should be automatically launched.  If you want to make the program be something like a Kiosk mode that takes up the whole screen, substitute the line w.show() with w.showFullScreen().

Here’s the updated code to do the Kiosk mode:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui

def main():

    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.showFullScreen()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

This code doesn’t include a good way to exit the program so the full screen mode will be stuck. Click Alt+F4 to terminate the program (just like Windows!?!?).

*Note on the new image of Jessie that comes with PIXEL desktop, the Pixel Version of Desktop seems to use a different start up file. We need to edit /home/pi/.config/lxsession/LXDE-pi/autostart instead.

$sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

Add the the same line to the bottom of the file

@sudo python /home/pi/render.py

Now when you reboot, it should launch our render.py program.