Raspberry Pi Scripts
Here is a collection of Raspberry Pi scripts that I’ve put together over the past few months to make running and recovery easier and things just generally a little brighter when using ssh on my Raspberry Pi.
My Raspberry Pi scripts
Make no bones about it – the Raspberry Pi scripts here are by no means advanced, but they work without errors and help things keep running smoothly.
That said, I like ’em.
Initialisation script
Bar far the handiest of my Raspberry Pi scripts – initialise.sh.
This script is saved in your user’s home directory as initialise.sh and I run it after a reboot or power outage.
I used to have problems with my Owncloud installation as the disk is a USB key and it was having trouble mounting properly so I started with that and built it up from there.
The script does the following (in order):
- Start a VNC server with a screen size of 1024×768 in 24bit colour depth and 96dpi.
- unmount, mount and check the HFS+ filesystem of my USB key
- restart Apache to allow Owncloud to run properly.
After a restart, that gets everything running smoothly again. As I said – handy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#!/bin/sh ### BEGIN INIT INFO # Provides: myscript # Required-Start: $local_fs $network # Required-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: myscript # Description: Start VNC and mount OwnCloud Drives ### END INIT INFO # # A script to set up a couple of bits on my Pi # Such as VNC, Owncloud, and to unmount, fsck and mount # USB Disk and /dev/sda1. Also restarts Apache2. # # Define text colours RED='\033[0;31m' #Red GR='\033[0;32m' #Green BL='\033[1;34m' #Blue NC='\033[0m' # No Color (Au Naturale) echo "" echo "Raspberry Pi Initialisation Script" echo "(C) David Hall 2015" echo "" echo "Script being run by ${GR}$USER${NC} on ${GR}$(date)${NC}" echo "" echo "${BL}Starting VNC Server${NC}" echo "" # Start VNC Server vncserver :1 -geometry 1024x768 -depth 24 -dpi 96 > /dev/null # If loop checks completion status of the last command # If $? = 0, the command was successful. 1 for failure. if [ $? -eq 0 ]; then echo "${GR}*** VNC Server Started ***${NC}" echo "${BL}Connect using the form x.x.x.x:1${NC}" echo "" else echo "${RED}*** VNC Server Not Started ***${NC}" echo "${RED}You will not be able to connect to VNC${NC}" echo "" fi echo "${BL}Mounting OwnCloud drive${NC}" echo "${BL}Unmount USB Disk" # Unmount /dev/sda1 sudo umount /dev/sda1 > /dev/null if [ $? -eq 0 ]; then echo "${GR}*** Drive unmounted successfully ***${NC}" echo "" else echo "${RED}***Drive unmounted unsuccessfully ***${NC}" echo "" fi echo "${BL}Check Filesystem on USB Disk${NC}" # Check volume on sda1 - HFS+ formatting on this system sudo fsck /dev/sda1 echo "${BL}Mount USB Disk For OwnCloud${NC}" # mount /dev/sda1 to /media/owncloud sudo mount /dev/sda1 /media/owncloud > /dev/null if [ $? -eq 0 ]; then echo "${GR}*** Drive mounted successfully ***${NC}" echo "" else echo "${RED}*** Drive mounted unsuccessfully ***${NC}" echo "" fi echo "${BL}Restarting Apache${NC}" # Restart apache to enable owncloud to use the USB disk sudo service apache2 restart > /dev/null if [ $? -eq 0 ]; then echo "${GR}*** Apache started successfully ***${NC}" echo "" else echo "${GR}*** Apache started unsuccessfully ***${NC}" echo "" fi echo "${BL}You're ${GR}good${BL} to go!${NC}" echo "" |
This script will look like this when run:

MOTD
This script replaces the .bash_profile in your user’s home folder. When it come to Raspberry Pi scripts, the MOTD is the one you will see most often (Especially if you’ve figured out cron!) so it’s a good idea to make it useful. Pretty also helps.
Whenever you log on the your Raspberry Pi using SSH (or when your not using graphical mode), .bash_profile runs by default and the message displayed above the user prompt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
### BEGIN INIT INFO # Provides: Message Of The Day (MOTD) # Required-Start: $local_fs # Required-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: # Description: Message Of The Day (MOTD) - Includes System information, local weather info and a reminder to run the post reboot script manually. ### END INIT INFO let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)" let secs=$((${upSeconds}%60)) let mins=$((${upSeconds}/60%60)) let hours=$((${upSeconds}/3600%24)) let days=$((${upSeconds}/86400)) UPTIME=`printf "%d days, %02dh%02dm%02ds" "$days" "$hours" "$mins" "$secs"` # get the load averages read one five fifteen rest < /proc/loadavg echo "$(tput setaf 2) .~~. .~~. `date +"%A, %e %B %Y, %r"` '. \ ' ' / .' `uname -srmo`$(tput setaf 1) .~ .~~~..~. : .~.'~'.~. : Uptime.............: ${UPTIME} ~ ( ) ( ) ~ Memory.............: `cat /proc/meminfo | grep MemFree | awk {'print $2'}`kB (Free) / `cat /proc/meminfo | grep MemTotal | awk {'print $2'}`kB (Total) ( : '~'.~.'~' : ) Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min) ~ .~ ( ) ~. ~ Running Processes..: `ps ax | wc -l | tr -d " "` ( : '~' : ) IP Addresses.......: `/sbin/ifconfig eth0 | /bin/grep "inet addr" | /usr/bin/cut -d ":" -f 2 | /usr/bin/cut -d " " -f 1` and `wget -q -O - http://icanhazip.com/ | tail` '~ .~~~. ~' Weather............: `curl -s "http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=EUR|UK|UK001|EDINBURGH|" | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p' ` '~' $(tput sgr0) Don't forget to run 'sudo ./initialise.sh' to start VNC, etc. Free Disk Space....: `df -Pk | grep -E '^/dev/sda1' | awk '{ print $4 }' | awk -F '.' '{ print $1 }'`k on /dev/sda1 " |
When run, it should look a little like this:

So there you have it – a wee collection of Raspberry Pi scripts which I find useful.
Leave a Reply