BASH Basics: Introduction to BASH - Part 7

After all the theoretical stuff in the last few parts of this series, we’re going to take a breather and deal with clever ways to increase productivity in BASH when working with ODROID. The default Ubuntu MATE system is chock-full of useful utilities without requiring so much as installing additional programs. Now, let us take this unused potential for a spin.

Remote access options

If you read this article, chances are you want to access your ODROID not just with keyboard and mouse, but with a remote console on a different system. SSH is the established standard for this-- you cannot miss it since it is in every install and setup document for our single-board computers. But did you know that a full install of a graphical environment can be used even over the SSH connection?

Try to start it with:

ssh -X
when you access the system from another Linux computer. It is possible to make this work with Windows using MobaXterm, but explaining how to to do this is beyond the scope of this particular article.

If you have a lightweight image viewer such as fbi, fim, or feh installed, you can now enter

fim /usr/share/backgrounds/ubuntu-mate-photos/
to browse the MATE backgrounds over the remote connection. You can also use the MATE image viewer eom if you like, but lightweight alternatives are faster over a remote connection. Advance with SPACE, quit with q.

From the command line, you can start any graphical application on your ODROID system. Since the programs are started on the ODROID, the file open dialogs also open the ODROID file system and work with the remote files.

Getting remote audio files to play locally is surprisingly difficult. Instead of using complicated setups, we cheat here by using SSHfs (install with apt install sshfs). Make or use an empty folder:

  • mkdir ~/remoteODROID on your local Linux computer which you use to access the ODROID.
  • With sshfs ODROID@myODROID.lan:/home/ODROID ~/remoteODROID, you can mount the ODROID home directory directly in the local ~/remoteODROID folder, without the need for setting up networking with NFS first.

Everything is mounted now and can be used like a local file so you can listen to audio files and look at video files on the ODROID without much hassle. Don’t forget to unmount with

fusermount -u ~/remoteODROID
after you are done.

Running processes in the background

One peculiarity of the SSH connection is that if you close the connection, running processes get aborted. Let’s say you put your new BASH scripting skills to good use and made a small script to record the temperature of the ODROID. You want to see if the system heats up too much in a new environment, stress test, or in a closed case, so you let it run for one hour:

templogger.sh

#!/bin/bash
# This script logs system temperature for one hour
# A file 'temp.log' in the home directory is used and overwritten

if [ -f ~/temp.log ]; then
  rm ~/temp.log        #if file exists, remove it
  touch ~/temp.log     #create file
fi
  for run in {1..720}  #run 720 times (5s interval)
do
  cat /sys/devices/virtual/thermal/thermal_zone0/temp >> ~/temp.log
  #for XU4/HC1/HC2; adjust for own system
  echo $run
  sleep 5
done
If you close the connection by shutting down the PC so you can get a cup of coffee, the test stops. To prevent this, you can background a task with bg and foreground it with fg, but backgrounding and foregrounding is cumbersome. Easier and more versatile is the program tmux or its predecessor screen. We are just touching the basics here, but you can split the screen with tmux and make a two- or three-pane-view, amongst other goodies. Take a look at the manual with man tmux, but don’t get overwhelmed.

Install it as usual with apt install tmux. Before you start your temperature logging, start tmux first, just by entering tmux. You’ll notice a status line at the bottom that contains, from left to right, the name of the current session in square brackets; the window list; the title of the active pane in double quotes; and the time and date.

Figure 1 - A tmux screenshot

If you start the templogger.sh script now, you can detach the tmux session with CTRL+B and d for detach. Just a reminder: If you put all your scripts and programs in the ~/bin directory, they are automatically in your path. Now close the SSH connection with CTRL+D and the local terminal as well with CTRL+D. You may even shut down your PC and get the cup of coffee I mentioned earlier.

When you remote into the ODROID again with SSH, you’ll get the normal welcome screen with the message of the day. Where is your running script? A simple tmux attach brings back the tmux session window, and you can see how many data points were already recorded in the meantime.

Repeat the procedure above to detach the session again. When you are finished, a simple quit or CTRL+D exits tmux until you need it again. Whenever you need to do operations which run for hours, or even indefinitely, tmux is a godsend. Before we explore productivity further, let’s have a little fun with the visuals.

Beautification

The tmux status line is a reminder that well-placed status information is helpful for not getting lost in our system. We learned in the beginning about customizing the BASH prompt, but let’s face it, all the options mentioned have been around for decades, and look accordingly. The world has evolved beyond pure ASCII now.

What can we do to have a nicely formatted console with options that look fresh and new? It’s not as easy as just changing a PS3 prompt in .bashrc. However, a lot of people have already put a lot of time into this and made a nice .deb package for us to use. Install it with apt install powerline first. You’ll notice that nothing appears different. We have to finish with some system modifications as well.

Open your .bashrc file with your favorite text editor and go near the end. Before adding custom functions, the standard .bashrc ends with a block starting with the following line:

#enable programmable completion features
and ending with the following line:
fi

After this block, insert the following text:

# Powerline features for bash, based on Ubuntu packages
if [ -f `which powerline-daemon` ]; then
  powerline-daemon -q
  POWERLINE_BASH_CONTINUATION=1
  POWERLINE_BASH_SELECT=1
  .  /usr/share/powerline/bindings/bash/powerline.sh
fi
Save the file, exit the shell with CTRL+D and open it again. Voilà! You get a fresh look which makes working with the console and BASH even easier.

Figure 2 - Powerline BASH

References

Powerline BASH script: https://github.com/brujoand/sbp Add power to your terminal with Powerline: https://fedoramagazine.org/add-power-terminal-powerline/

Be the first to comment

Leave a Reply