Auto Run Terminal Program on Boot

Raspberry Pi Info

By David Efflandt


With the Raspberry Pi being relatively inexpensive and customizable by just booting a different SD card, the question came up how to it boot up and automatically run a program from a virtual terminal. That could lend itself to booting into a kiosk, monitoring program, or in this case a game. Programs or scripts that run during boot typically have no controlling terminal and nobody is logged into the initial virtual terminal.

A websearch brought me to Automating Program Startup which farther down the page about Autologin reminded me about inittab and included a link to an autologin script. I just adapted that info to Raspbian files on the Raspberry Pi, testing the autologin, then launching a program (quake3).

For something like this that runs automatically, you probably want to configure a different user with limited authority, so if they get into the shell when you are not looking, what they can do there is limited. It goes without saying that you should also change the default password of user "pi".

Right click on autologin script and download it
The script works as is, but requires ksh shell
cd to the directory containing autologin (~/Downloads?)

sudo cp autologin /usr/local/bin/autologin
sudo chmod +x /usr/local/bin/autologin
sudo apt-get update && sudo apt-get install ksh
sudo adduser quake3 (or whatever user)

Add that user to any groups they need for that program, which for games might be: audio, video, plugdev, games, input.
If not familiar with vi, you could use sudo EDITOR=nano vigr /etc/group.
Each group is a comma separated list like this:

audio:x:29:pi,efflandt,quake3

Then sudo nano /etc/inittab. If you want the 3-fingered salute to halt instead of reboot, change -r to -h here:

# What to do when CTRL-ALT-DEL is pressed.
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

Here comment out (#) the line that normally starts tty1 and add a line like this for the user you added:

#1:2345:respawn:/sbin/getty --noclear 38400 tty1
1:2345:respawn:/usr/local/bin/autologin quake3 tty1 38400

Reboot to make sure that the terminal automatically comes up with that user already logged in. One thing that the autologin script fails to do is set TERM which is needed to be able to use a text editor, so type:

export TERM=linux

Then edit the ~/.profile of that user to set TERM before it sources .bashrc, and add any commands at the end for whatever program you want to run.

Example of ~/.profile which defines TERM and runs quake3:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# not defined by autologin
export TERM=linux

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi

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

# run quake3 & return to ~/ on exit
cd quake3/build/release-linux-arm
./ioquake3.arm
cd

One thing I have not set up here is a way to block someone from getting to the shell, which could allow them to delete any files they have read permission for. One option would be to end their .profile with logout, which would respawn their shell and autorun the program that they just tried to get out of.

Instructions for building quake3

If you have quake3 for PC, you can copy everything from its baseq3 directory into the baseq3 directory under the build tree including the .pak files, your CD key and q3config.cfg which should allow you to continue where you left off in the PC version with that username.