Raspberry Pi: Auto-starting a control system at boot time

Micro-sized computers, such as the Raspberry Pi, become increasingly popular as high-end replacements for microcontrollers. If a control program needs to be run in the Raspberry Pi, we would often like it to start automatically at power-up (much like a microcontroller would behave), or, more precisely, at boot time.

The information in this post can be found in various places on the Internet. It is nothing new, and I consider this to be a sort of yellow sticky note for myself — plus everybody else who might find this interesting.

Typically, the Raspberry Pi boots up and presents a login screen, either a text-mode console (TTY) or an X-window-based GUI. In either case, we need to manipulate the boot process. I assume that you (the reader) are familiar with text editors, the command-line interface, and with simple Linux behavior, such as the fact that you need to have root rights to edit files in /etc (and, for that matter, know what /etc means).

Step 1: Create the user account under which the program will run

You probably don’t want to run your application program with root privileges. Therefore, let’s create a dummy user under whose account the application will run. Let’s call that user “app”. Since we need that user to have a valid login, you also need to create a strong password. You know… if it looks like a Perl statement, you are doing something right. Note: After you added the user (e.g., with “adduser”), you may need to add this user to several groups to provide access to I/O functions. Relevant groups could be, for example, plugdev, dialout, or gpio.

Next, install the application program and verify that it works. You do this by logging in under the new user “app” and starting the program as you would normally do.

Step 2: Auto-login the new user

Now, we will modify the boot table so that the new user is logged in automatically whenever the Pi is booted up. The relevant file for this purpose is /etc/inittab. Normally, this file spawns six TTY console logins (getty) to allow normal login. Choose one console (TTY1 is the one that shows up on boot, but any other console works, too). Let’s assume we log in the user on TTY2. Fairly far down in /etc/inittab, you’ll find a block that looks like this:

1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
4:23:respawn:/sbin/getty 38400 tty4
5:23:respawn:/sbin/getty 38400 tty5
6:23:respawn:/sbin/getty 38400 tty6

The first column is the TTY number. The second is the runlevel under which this TTY is activated. The third column is the action (respawn) and the fourth is the command to respawn. We now change the second line to

2:23:respawn:/bin/login -f app tty2 < /dev/tty2 > /dev/tty2 2>&1

Instead of starting a getty on TTY2, we now start a login (the -f flag specifies the user name). Also, we redirect standard input and output from/to TTY2 (i.e., the device /dev/tty2). Note also that we don’t really need “app”‘s password here. The password is useful when you need to manually login to troubleshoot.

If you reboot the system now, you should see that user “app” is automatically logged in on TTY2. Neat.

Step 3: Auto-start the application when “app” logs in

There is a file in each user’s home directory, named .profile, which is executed by the login shell. Note: If the login shell is bash, make sure that neither .bash_profile nor .bash_login exist, because those override .profile.

Complete this step by editing /home/app/.profile and appending the command to start your application. This is what it could look like towards the end of .profile:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then

# Autostart the control program


The actual start of the application is now performed in a script called “start_my_control.sh”, and you can use that script file to set any environment variables, provide command-line arguments etc. as needed.

What to do when the program has a GUI and requires X window?

Disable the automatic start of the X window system on TTY7 and instead use “startx” to start the X window system from the user’s login in Step 3. Then use the window manager’s autostart feature, which depends on the window manager (xfce? lightdm?) you installed.

Leave a Reply

Your email address will not be published. Required fields are marked *