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.

Share

    Recommendations

    • Toys Contest

      Toys Contest
    • Big and Small Contest

      Big and Small Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    6 Discussions

    0
    None
    DarrenB35

    7 months ago

    Thank you so much, JaredT22. I've been scouring the internet to figure out a way to get a Raspberry Pi with a media server to update automatically. This ended up being my starting point, but I tried this in Raspbian Stretch and had some serious problems getting the shell script to run. I'm brand new to Linux, so by no means am I an expert. Things may have changed between Jessie and Stretch (and will likely continue to change) but in figuring out how to get this to run properly, I ended up doing a lot of research across the web. This is what I found:

    1) The default shell in Stretch is bash, so the shebang at the first line of the shell script now needs to be

    #!/bin/bash.

    2) sudo rpi-update will pull buggy, experimental firmware onto the Raspberry Pi, so I took out that command altogether.

    3) For some reason, both the shell script and the log file that I created were owned by root. While I could have gotten around this problem by using sudo chown to change the owner, there was an easier way. cron has a user-specific version and a root version, so I activated the root cron using the command

    sudo crontab -e

    4) I wanted to separate and date each upgrade and include subheadings to allow for greater scrutiny of the log. There are also a few more steps in the update/upgrade/cleaning process that require the user to input a y/n choice, so my final shell script looked like this:

    #!/bin/bash
    echo "**********" >> /logs/crontab
    `date` >> /logs/crontab
    echo "UPDATING SYSTEM SOFTWARE - UPDATE" >> /logs/cronlog
    sudo apt-get update
    echo "UPGRADING SYSTEM SOFTWARE -
    UPGRADE" >> /logs/cronlog
    sudo apt-get –y upgrade
    echo "UPGRADING SYSTEM SOFTWARE -
    DISTRIBUTION" >> /logs/cronlog
    sudo apt –y dist-upgrade
    echo "REMOVING OBSOLETE
    DEPENDENCIES" >> /logs/cronlog
    sudo apt-get –y autoremove
    echo "REMOVING OBSOLETE FILES"
    >> /logs/cronlog
    sudo apt-get autoclean
    echo "SYSTEM REBOOT" >> /logs/cronlog
    sudo reboot

    I hope this helps anyone who runs into the same difficulties that I did!

    0
    None
    TkacP

    11 months ago

    Will this work when there will be update requiring user's choice? LIke PHP updates like to ask about changes in php.ini. And the default option (use new php.ini) is NOT the correct one, as you usually want to keep existing php.ini. How to overcome this problem? Isn't "unattended-upgrades" better solution?

    0
    None
    ErikP58

    1 year ago

    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.

    0
    None
    JRV31

    2 years ago

    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.

    1 reply
    0
    None
    JaredT22JRV31

    Reply 2 years ago

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

    0
    None
    JRV31

    2 years ago

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