Monthly Archives: December 2015

Synchronizing system clock without ntp

I have a very small system running on Busybox and I need to maintain the correctness of system time within a few seconds. Without synchronization this system clock would drift around 10 seconds every 24 hours. My solution was to create two shell scipts, one on server and the other one on the target system (system with Busybox).

The script on the server executes command “date” and save the output as a text file and send that file using ftp to the target system. And the script on target system will read that file and set the system time accordingly. Unfortunately with wireless network I could not predict the file transfer delay time so I had to build a mechanism to add time compensation.

Here is the script on server:

#!/bin/bash
##########################################################################
#This script writes an actual time as a text file on target system.
#This text file will be read by the script on target system
#and be used to synchronize the system time.
#Created by Okki on 07-Dec-2015
##########################################################################

HOST=my.target.system.ip #This is the FTP servers host or IP address.
USER=username            #This is the FTP user that has access to the server.
PASS=password            #This is the password for the FTP user.
ADDSEC=10                #Additional seconds for FTP delay compensation.

#Split the date information into "hh:mm:ss" and write it in file.
#date | awk '{split($0,array," ")} END{print array[4]}' > time.txt
#echo "10:10:10" > time.txt

TIME=$(date | awk '{split($0,array," ")} END{print array[4]}')
HH1=$(echo $TIME | awk '{print substr($0,1,1)}')
HH2=$(echo $TIME | awk '{print substr($0,2,1)}')
MM1=$(echo $TIME | awk '{print substr($0,4,1)}')
MM2=$(echo $TIME | awk '{print substr($0,5,1)}')
SS1=$(echo $TIME | awk '{print substr($0,7,1)}')
SS2=$(echo $TIME | awk '{print substr($0,8,1)}')

HH=$(echo $HH1$HH2)
if [ $HH1 -le 0 ]
then
  HH=$(echo $HH2)
fi

MM=$(echo $MM1$MM2)
if [ $MM1 -le 0 ]
then
  MM=$(echo $MM2)
fi

SS=$(echo $SS1$SS2)
if [ $SS1 -le 0 ]
then
  SS=$(echo $SS2)
fi

let "SS += $ADDSEC"

if [ $SS -ge 60 ]
then
  let "SS -= 60"
  let "MM += 1"
fi

if [ $MM -ge 60 ]
then
  let "MM -= 60"
  let "HH += 1"
fi

if [ $HH -ge 24 ]
then
  let "HH -= 24"
fi

if [ $SS -lt 10 ]
then
  SS=0$SS
fi

if [ $MM -lt 10 ]
then
  MM=0$MM
fi

if [ $HH -lt 10 ]
then
  HH=0$HH
fi

# Write compensated time into text file
echo "$HH:$MM:$SS" > time.txt

# FTP function calls:
# Call 1: Uses the ftp command with the -inv switches.
#-i turns off interactive prompting.
#-n Restrains FTP from attempting the auto-login feature.
#-v enables verbose and progress.
ftp -inv $HOST << EOF

# Call 2: Here the login credentials are supplied by calling the variables.
user $USER $PASS

# Call 3: Here you will change to the directory where you want to put or get.
cd /system/target/path

# Call 4: Here you will tell FTP to put or get the file.
put time.txt

# End FTP Connection.
bye

EOF

And here is the script on target system:

#!/bin/sh

while true ; do
  TIME=$(date | awk '{split($0,array," ")} END{print array[4]}' | sed 's/:/-/g')
  TIME_OLD=$TIME
  # Wait until time changes
  while [ "$TIME_OLD" == "$TIME" ] ; do
    # Check if timesync file from linux server exists
    # and has size greater than zero (ftp copy delay!)
    # Time format hh:mm:ss
    if [ -s time.txt ]
    then
      date -s $(cat time.txt)
      rm time.txt
      # Set system time
      hwclock -w
    fi
    TIME=$(date | awk '{split($0,array," ")} END{print array[4]}' | sed 's/:/-/g')
  done
done # Endlosschleife...

To make this process automatically executes every day, I added following line on Crontab:

10 12 * * * root /path/to/script.sh > /dev/null 2>&1

Line above tells crontab to execute script.sh every day on 12:10.

Display special characters for html

Sometimes we need to create a static html page which displays words with special characters like Ä, ä, Ö, ö, Ü, ü and ß. In order to make these characters can be displayed correctly by webbrowser, we need to convert them in html special character format. I use awk to convert all of those characters into html format.

In this example I need to list file names in a folder and save it as html file. Here is my code:

ls -la /path/to/folder/ | awk '{ 
 gsub (/ /,"\\&nbsp;")
 gsub (/ä/,"\\&auml;")
 gsub (/ö/,"\\&ouml;")
 gsub (/ü/,"\\&uuml;")
 gsub (/ß/,"\\&szlig;")
 gsub (/Ä/,"\\&Auml;")
 gsub (/Ö/,"\\&Ouml;")
 gsub (/Ü/,"\\&Uuml;") ;print $0"<br>"}' >> /path/to/filename.html

With that code above I can see a list of files using webbrowser and get appearance as if I would list it using linux terminal.