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 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:

#synchronizes DTS and updates all packages

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

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 which located in my home directory. Also, you need to make it executable before running:

$ chmod +x ~/

Before use it you should install ntpdate utility using command:

$ sudo apt-get install ntpdate

After all operations you can run it just typing:

$ ~/

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 Also, you can download to your RPi directly using wget:

$ cd ~
$ wget
$ chmod +x ~/


No comments:

Post a Comment