Saturday, October 22, 2016

Raspbian: Script For Synchronizing Date & Time And Updating All Packages (for beginners)

Raspbian: Script For Synchronizing Date & Time And Updating All Packages (for beginners)



Here is my system-update.sh script which able to do these operations:
  • synchronize date & time (DTS) via NTP;
  • write synchronized DTS to my RTC (DS3221) *;
  • update all installed packages.
* - if you want to know how to attach DS3231 (RTC) to your RPi you can read my post Installing hardware RTC (DS3231) to Raspberry Pi



So, all script data (last version) which able to do all described operations is:

#!/bin/sh
#synchronizes DTS and updates all packages

echo "Synchronizing date & time..."
sudo ntpdate -s time.nist.gov

echo "Updating RTC..."
sudo hwclock -w

echo "Updating all packages..."
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get dist-upgrade

echo "Done!"


This data is stored in file system-update.sh which located in my home directory. Also, you need to make it executable before running:

$ chmod +x ~/system-update.sh

Before use it you should install ntpdate utility using command:

$ sudo apt-get install ntpdate

After all operations you can run it just typing:

$ ~/system-update.sh

Script execution can take a long time for execution, depends on channel throughput and current packages versions. Note that when you running this script first time for just installed OS it can take extra-long time for execution. So, just relax.

If you don't like ntpdate you can use this sequence of commands for DTS synchronization:

$ sudo service ntp start
$ sudo ntpd -gq

$ sudo service ntp stop

These three lines enabled NTP daemon (service), updates DTS and disables NTP. Note that after these three strings your NTP will be disabled (I prefer do not use it when working with RPi). Bold line actually updates DTS so you can use it separately.

Also, note that updating procedure may require your attention and ask for confirmations for configuration update and packages replacement.

You also download this script here system-update.sh. Also, you can download to your RPi directly using wget:

$ cd ~
$ wget http://files.danish.com.ua/downloads/blog/2016-10-22/system-update.sh
$ chmod +x ~/system-update.sh

Enjoy!

No comments:

Post a Comment