Start a program at boot up

I have no keyboard and display connected to my Raspberry Pi (running Raspbian), but log in to it over the network by using ssh.

Now, I wanted to automatically start a program when the Raspberry Pi boots up, but also be able to see the console output of that program when needed.

To accomplish that, I’ve done the following:

  • sudo apt-get install mingetty
  • sudo apt-get install screen
  • Edit /etc/inittab, and changed the part with getty’s to autologin on tty3 with mingetty as user pi
...snip...
# /sbin/getty invocations for the runlevels.
#
# The "id" field MUST be the same as the last
# characters of the device (after "tty").
#
# Format:
#  :::
#
# Note that on most Debian systems tty7 is used by the X Window System,
# so if you want to add more getty's go ahead but skip tty7 if you run X.
#
1:2345:respawn:/sbin/getty --noclear 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
T:23:respawn:/sbin/mingetty --autologin=pi tty3
...snip...
  • Add the following to /home/pi/.profile, to autostart a program when user pi login on tty3
if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty3 ]; then
	screen -d -m /home/pi/tstat
fi

So, when the Raspberry Pi boots, user pi will automatically log in and run the program tstat. Because tstat is started with screen, I can log in with ssh and run screen -R to see the output of tstat and press ctrl-a d to exit screen, but leave tstat running.

login and typing screen -R

output of tstat

After pressing ctrl-a d

3 comments for “Start a program at boot up

  1. Vincent Demers
    March 28, 2013 at 4:15 pm

    Thanks a lot, this is exactly what I want to achieve !

  2. Åke Johansson
    July 14, 2014 at 10:16 am

    What if I have to be root to run autostarted program ? I can´t get that to work.

    • July 21, 2014 at 4:24 pm

      I’m busy with other things at the moment (moving to a new house, etc …), so, I’ve not a Raspberry Pi at hand to test things. But I suppose you can try to use SUDO in some scripts.

Leave a Reply

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