USB Console Script

After connecting up my Raspberry Pi to my laptop using a USB serial console cable I found that I kept missing the initial console output.

This is because to connect to the serial interface the device must be present on the system, so you have to plug the USB serial console cable in and then enter the command to connect to the device. Of course as soon as you plug it in Raspberry Pi it starts to boot so you miss the initial output.

Note – To be able to connect to a serial device (including a USB serial interface you need to be a member of the ‘dialout’ group.

$ sudo usermod -a -G dialout username

What I needed was a command that I could run before plugging in the USB cable that would wait for the device to appear and then connect automatically, and a way to delay the start of the boot process by just a few seconds to give the script time to connect.

To do this I wrote the following script that loops waiting for the USB serial interface to be created for 15 seconds and connects using the screen utility when it is available.

# [<device>]
# Waits  for  a  USB  serial device to become available then  connects  to  a
# console session using 'screen'. 
# Note - If you close the window without quiting 'screen' it will continue to
# run in the background and prevent new connections being made.  Not sure how
# to fix that - but killing the offending process using 'kill -9' works!
# This  program  is free software: you can redistribute it and/or  modify  it
# under the terms of the GNU General Public License as published by the  Free
# Software  Foundation, either version 3 of the License, or (at your  option)
# any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY  WARRANTY;  without  even the implied warranty  of  MERCHANTABILITY  or
# FITNESS  FOR A PARTICULAR PURPOSE. See the GNU General Public  License  for
# more details.
# You  should  have received a copy of the GNU General Public  License  along
# with this program. If not, see <>

# I use a list of valid devices to check the device specified by the user
# is correct, as it won't actually exist until it is plugged in.

devices="/dev/ttyUSB0|/dev/ttyUSB1" # Valid devices.
default="/dev/ttyUSB0" # Default device.
command=screen # Command for console session.

status=1 # Return an error status by default.
if [ -z "$2" ]; then # Check no extra parameters exist.
  command=$(command -v $command) >/dev/null 2>&1 # Command exists?
  if [ -x "$command" ]; then # Is command executable?
    if [ -z "$device" ]; then # Fall back to the default device.
    if [[ $device =~ ^($devices)$ ]]; then # Is the device 'valid'.
      while [ ! -c "$device" -a $count -gt 0 ] # Wait for device.
        echo -ne "Waiting ($count""s) for $device  $string \r"
        sleep 1
        count=$((count - 1)) # Decrement counter.
      if [ $count -gt 0 ]; then 
        $command $device 
        status=$? # Update status
      echo "Device $device is invalid."
    echo "Cannot execute $command"
  echo "Too many parameters."
exit $status

To open a console window on the desktop I have a desktop shortcut with the following launcher properties

gnome-terminal –geometry=80×25 –window-with-profile=”Console”
–title=”USB Console” –execute screen $HOME/

You could use this script to connect to a regular serial device as well by including the regular serial port devices in the list of ‘valid’ devices.

To get the Raspberry Pi to pause for a couple of second while the script connects I logged in as root and added an additional setting into config.txt.

# vi /boot/config.txt


This will cause the Raspberry Pi to pause for three seconds while booting, this is enough to ensure that the USB device is detected and I can connect reliably but short enough to not be to intrusive when starting up normally or rebooting.

The other lines are specific to my setup and I suggest you ignore them.

Raspberry Pi is a trademark of the Raspberry Pi Foundation

This entry was posted in Linux and tagged , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s