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

Raspberry Salad Part 2: Raspberry FM
Raspberry FM : Project finale
  • Trackback are closed
  • Comments (12)
  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 😀

    • 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!

    • just me
    • December 2nd, 2014 5:23am

    Where do i put my password in the autologin part? If i dont put password it wont login but gives errors and 5 minute freeze

  2. I’m not sure: By doing what I did above (under “Auto login to the Pi:”), I didn’t need to enter the password, just my username, and it autologged fine. Maybe you’ve setup a different username with different login permissions? I’m not Linux expert so unfortunately my knowledge doesn’t extend much past this :(

    • RaspPi4XMas
    • December 30th, 2014 6:48am

    Hi all.
    This doesn’t work on my Pi.
    Changes in inittab are well done and saved but login is still required on startup.
    The same happens when I change parameters in config.txt for example.
    Changes I do are saved but seems to have no effect at all
    I’m using Noobs. Is it possible that Noobs somehow overrides my startup parameters with its owns ?

  3. I’m personally not sure why it wouldn’t, I’m a Linux noob myself.
    That being said, I too use the Noobs distro, and the above setup works for me. Not that it helps you directly, but I don’t think it’s a Noobs issue.

    • RasPi4XMas
    • December 31st, 2014 8:03am

    OK, thanks for your response.
    I’ll continue investigating as to why it doesn’t work on my Pi then.

    • Lickity40
    • January 17th, 2015 6:24am

    Not sure if this will help, but it appeared that I wasn’t logged in either. But when ran who, here were the results:

    Remote in via putty. Logged in with my pi creds.

    pi@face-pi ~ $ who
    pi tty1 2015-01-17 09:06
    pi pts/0 2015-01-17 09:12 (my-laptop.local)
    pi@forsythe-pi ~ $

    This shows that pi is logged in twice!

    • oakie22
    • February 2nd, 2015 8:49am

    This is working great! My B+ Pi was purchased in November, 2014. My application was not to play music, but rather to launch omxplayer for video. My Pi plays the rtsp stream coming from an Axis IP camera. The video plays on a TV via HDMI. Thanks for this series of articles!

    • john
    • September 2nd, 2015 2:56pm

    Did not work, and broke my cubieboard. Had to reinstall from scratch. Lost everything. :(

  4. Sorry to hear that: I’ve done this process on a couple RPI’s now with no issue. But the CubieBoard is different hardware?

Comment are closed.