Raspberry Salad Part 3: Auto-run program on startup

Continuing from my previous post of turning my Raspberry Pi and Makey Makey into a ‘internet radio tuner’, the next step is to get the RPi to auto-login and start playing music when booted:  I want this to be a standalone piece of hardware that won’t require a monitor and keyboard every time I want to use it.

Coming from the embedded microcontroller world of Arduino, I’m used to powering the hardware on, and it running “the code” I’ve programmed for it.  The Raspberry Pi is its own little Linux computer:  When it turns on, by default it expects a user to log-in, via password.  Then, it expects the user to “do stuff” on the computer.  But since the RPi is so small, it’s perfect for embedded systems like the Arduino.  But once embedded, how do you have it auto-login and execute code of your choosing?  Like many others on the web I encountered this issue, and like many others, I’m blogging about how I figured it out.  This is all completely new territory for me, everything was done via searching the web and trying it out.

Auto login to the Pi:

The first thing to solve is how to get the RPi to auto-login, thus removing the necessity of a connecetd keyboard & monitor (or ssh) to get things running.  This doc completely covers it: http://elinux.org/RPi_Debian_Auto_Login.  But to paraphrase, this is what you need to do:

Edit /etc/inittab , the  file that controls the startup \ initialization process of the Pi.  Do a “What is /etc/inittab” in Google for a far more robust answer on the specifics of what it does.

$ sudo nano /etc/inittab

In that file, comment out this line:

1:2345:respawn:/sbin/getty 115200 tty1

Like so:

#1:2345:respawn:/sbin/getty 115200 tty1

And place this line under it:

1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

Replacing “pi” with the user you want auto-logged on on power-up.  Reboot the Pi, and watch it auto-login.  Magic.

Bash scripting for noobs

Before you can create a starup script, you need to know a bit about how to author it.  And when I say noob, I mean me:  I’ve done plenty of scripting in Python, Java, Mel, etc… but never in Bash.  Here’s basic stuff you need to know to make your scripts work (based off of info pulled from the web.  Repeat:  I am no expert at this):

  • The name must end in .sh
  • The first line must be : #!/bin/bash
  • You’ll need to make the script executable: $ chmod ugo+x startup.sh
  • To execute the script, you need to run it via bash:  $ bash startup.sh
  • Use ‘echo’ to print strings in the script.

How to auto-start a script on (auto) login:

I found a pretty good overview from this post:
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=29&t=7192
I’ve sort or reorganized the posts to describe the different ways to o it:

/etc/rc.local : Add something to this file if you want it to execute every time the RPi is booted or rebooted.

  • Putting something into /etc/rc.local adds it to the boot sequence. The word “sequence” is important, if your code gets stuck then the boot sequence cannot proceed.
  • It is good for starting extra services, doing it this way will give you an output to the display [and is what I need] and also the ability to stop the script drop to a terminal and restart it with ease .
  • It is also true that when you put it into the /etc/rc* sequence, if it fails, it gums up your booting process (and this can be fatal). You have to be careful what you put there. Human frailty and all that…
  • Put stuff in /etc/rc.local if you want it to run on boot – it will run with root privileges (I think you could use sudo to stop that), and will run before login.
  •  You can see it running right there in the virtual console – and this is critical if it is interactive.

~/.bashrc : Add something to this file if you want it to execute every time a specific user logs in.

  • The same with rc.local, if you put a faulty command in here you may not be able to login.

/boot/boot.rc

  • Note that the RPi may have a file on the FAT partition named /boot/boot.rc which performs the same function as /etc/rc.local. This is safer because you can edit this partition from any computer.
  • In my case, I didn’t have this.

Based on that info, I decided to use the ~/.bashrc method (shown below):  In case I do something terrible, I could always login as another user (pi) and try to rectify the situation.

Author the startup script

To make this terribly obvious, I created a startup.sh script in my home dir, running the code I wanted to execute on login.  This is a modified version of the mplayer plalylist launcher code from my previous post, passing in the absolute path to the playlist to launch:

$ nano ~/startup.sh
#!/bin/bash
echo startup.sh : Launching mplayer: somafm.pls
mplayer -playlist ~/mplayer/somafm.pls

Need to make it executable:

$ chmod ugo+x startup.sh

Now that the startup script is created, edit ~/.bashrc to execute it:

$ nano ~/.bashrc

Go to the last line and add:

# Run the startup bash script:
echo .bashrc : Running startup.sh
bash startup.sh

Note I don’t give it a specific path to startup.sh:  Based on the fact startup.sh is in the home dir, it seems to execute fine from that location.  The echo statements in the above scripts are a bit extraneous considering for the most part a monitor is no longer needed, but I figured they’d be worth adding for troubleshooting purposes.

Listen to the music

At this point, reboot the Pi, plugin some headphones, and wait for the music to start streaming :)  If you’ve followed the instructions in the previous posts, you can use the Makey Makey to ‘change stations’ based on the authored playlist.

What’s next?  Packaging:  Need to come up with a slick way to package this, and provide a good tactile interface via the Makey Makey.

Go to previous: Raspberry Salad Part 2: Raspberry FM

Go to next post: Raspberry FM : Project finale

  1. Thanks for the clear write-up.

    • Yan
    • December 3rd, 2013 10:07am

    Best article in the world. Pulled me out the s**t!
    Thanks very much :D

    • Michael Burton
    • October 14th, 2014 7:59pm

    I’ve spent so much time messing with auto looper img’s from various contributors which seem to always bug out. Your simple instructions and a few omxplayer code tweaks make auto start/ looping vids easy! Thank you so much!

  1. No trackbacks yet.