Introduction: Raspberry Pi Auto Update (Updated)

Picture of Raspberry Pi Auto Update (Updated)

If you're like me, you want automatic updates for your Raspberry Pi setup. This of course would only be needed if you setup a media server like Plex, OSMC, or KODI.

This really takes the hassle of updating this server since the main purpose of a media server is to be a set-top box. There are a couple of ways to do this, but the one that will be used in the Instrucable is setting up a cron job.

First of all, I set my Raspberry Pi up using the inscrutable below:
https://www.instructables.com/id/Ultimate-Raspberry-Pi-Configuration-Guide/

This has been updated so that logging works properly.

Step 1: Step 1: Update.sh

You will need to create a shell script as a first step. This script will update everything on your Raspberry Pi when run. It will also remove and clean any files and dependencies that aren't being used. This will save some space on the SD card and is a good practice to ensure that you don't have anything unnecessary on your Raspberry Pi.

This script will also update your Raspberry Pi firmware.

First, create a file in the root user (pi if you kept this account) using the command below:

sudo nano update.sh

Add the following lines of code to the file and save it (ctrl + X, Y and enter). This will the basis for the automatic updates:

#!/bin/sh
sudo apt-get update && sudo apt-get upgrade -y 
sudo rpi-update 
sudo apt-get autoremove 
sudo apt-get autoclean 
sudo reboot

You will want to make the shell script executable using the following command:

sudo chmod +x update.sh

Step 2: Step 2: Cron

Optional: Create a folder to store a running log file for when this runs every week.

(This log folder and the file created below can be anything you want to name it and in any location)

sudo mkdir /logs

This will create a new folder or directory in location you're at (i.e. /home/pi/logs/). If you want to create a folder that is in root, just put a ~ in front of /logs.

To make the entry to cron, we will need to add the following to the crontab file using the following command and selecting option 2:

crontab -e

The cron job will look similar to what is below. This depends on if you
want a log file created after each run (remove '>/home/pi/logs/cronlog')This will run once a week on Saturday at midnight. To create the cronlog file, just enter the following (you may need to use sudo):

touch /logs/cronlog

Add the following line at the bottom and save (ctrl X, Y and enter):

0 0 * * SAT sh /home/pi/update.sh 2>/home/pi/logs/cronlog

The 2> redirects all output to the file so that you will have a complete log.

For examples on the syntax of this line of code, head over to

http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

Step 3: Step 3: Troubleshooting

One issue I ran into doing this had to mostly with installing software from the repository once it was updated, although this isn't all that related. The following command might not work:

sudo apt-get install <software>

You might have to specify the distro in order for them to install:

sudo apt-get install <software> -t jessie

If you want to view the log file that was created during the cron job, use one of the following commands:

cat /logs/cronlog
tail /logs/cronlog

Let me know about any other issues that come up.

Pictures to follow.

Comments

ErikP58 (author)2017-07-19

Hi Jared,

Thanks for these instructions. Very useful. I took the liberty to adjust to my needs because I want to do the release upgrade manually and I only want to reboot when necessary. So my update.sh looks like this:

#!/bin/sh
sudo apt-get -y update > logs/update.log
sudo rpi-update >> logs/update.log
sudo apt-get autoremove >> logs/update.log
sudo apt-get autoclean >> logs/update.log
if cat logs/update.log|grep -i reboot
then
type "Reboot required">> logs/update.log
sudo reboot
else
type "No Reboot required" >> logs/update.log
fi

update.log will be refreshed after each update.

JRV31 (author)2016-08-05

In the lines sudo nano update.sh and sudo chmod +x update.sh in step one the instructables editor added a <br> to the end of the line. This is a problem with the instructables editor. When coping code into the editor always proofread it carefully.

JaredT22 (author)JRV312016-08-05

Thanks for the tip. I wasn't aware of that little issue since this was my first Instructable. Updating now.

JRV31 (author)2016-08-05

I will have to try this, I have been meaning to learn cron.