LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Enterprise (https://www.linuxquestions.org/questions/linux-enterprise-47/)
-   -   'timeconfig' Is Back For CentOS 6 (https://www.linuxquestions.org/questions/linux-enterprise-47/timeconfig-is-back-for-centos-6-a-4175490053/)

RandyTech 01-03-2014 06:24 PM

'timeconfig' Is Back For CentOS 6
 
This is a bash script solution. Go look elsewhere if you want to Frankenstein your system with alien or legacy rpms.

The old 'timeconfig' TUI that was present from the original Redhat 'Diaper' release all the way up through the RedHat 'EL5' release now occupies too many terabytes on the OS installation so it had to be discarded from RHEL6 (and CentOS 6). Ok, just kidding. The real reason they abandoned it is, is uh, uh, uh. Sorry. Cannot think of any justification for discarding the command line utility. "Moronic" is the first word that comes to mind. Nonetheless, I need it and its *gone*!!

That's all just unacceptable to me so I wrote a 'timeconfig' bash script to replace the abandoned OS feature. Here is how you can get it back too. Please keep in mind this was created to run on CentOS 6.x (and probably higher), but in theory should also work on RHEL6 and higher.

1. Login as 'root' (or su -) to begin. I use a remote session (SecureCRT, PuTTY or similar).

2. Make sure your installation fits the bill and that you don't have 'timeconfig' on your system. You can do so by typing the following commands:
Code:

            cat /etc/redhat-release
            timeconfig

If if comes back RHEL6 or CentOS 6.x -AND- 'timeconfig' comes back "command not found" then safe to proceed.

3. If you place this script in an appropriate location then it can be run from the system root prompt same as the old 'timeconfig' utility. I placed it in the following path:
Code:

            /usr/local/bin
Granted a bash script is not a binary executable but hey, the path already existed in my installation so I'm using it. Works for me so it should work for you. Create the path if it is not there.
Code:

            cd /usr/local/
            mkdir bin

4. Change into that directory and vi to create the new file.
Code:

            cd /usr/local/bin/
            vi timeconfig

5. Here is the code. Copy it to your clipboard and paste it into your vi session.
Code:

#!/bin/sh
#
clear
printf '\e[33;40;1m'
trap '' 2
until [ "$question" = "Where is the Rye Toast?" ]; do
  region=
  zonePick=
  until [ "$answer" = "42" ]; do
    clear
    echo -n "            "
    date
    echo "    ***********  PICK WORLD REGION ***********"
    echo "                    ('q' to quit)"
    echo ""
    count=`find /usr/share/zoneinfo/ -maxdepth 1 -type d |wc -l`
    dirName=(". "`find /usr/share/zoneinfo/ -maxdepth 1 -type d |sort |sed -e 's%^.*/%%'`)
    tzDir=(`find /usr/share/zoneinfo/ -maxdepth 1 -type d |sort`)
    offset=$(( count / 2 ))
    count=$(( count - 1 ))
   
    for left in $(eval echo {1..$offset})
    do
      right=$(( left + offset ))
      ldummy=$left")"${dirName[left]}"                        "
      ldummy=${ldummy:0:25}
      rdummy=$right")"${dirName[right]}
      echo "        $ldummy  $rdummy"
    done
    echo ""
    echo -n "                  Pick a region: "
    read region
    if [[ ! $region = "" ]] ; then # Process user region
      header="    ************ PICK '"${tzDir[$region]:20}"' ZONE ************"
      clear
      echo ""
      echo "$header"
      echo "                    ('q' to quit)"
      zcount=`find ${tzDir[$region]} -maxdepth 1 -type f |wc -l`
      zName=(`find ${tzDir[$region]} -maxdepth 1 -type f |sort |sed -e 's%^.*/%%'`)
      tzFile=(`find ${tzDir[$region]} -maxdepth 1 -type f |sort`)
      echo ""
      case $region in
        3|4|6|8|9|10|13|14|16 ) ################## DISPLAY 1 COLUMN ####################
            zcount=$(( zcount - 1 ))
            for col1 in $(eval echo {0..$zcount})
            do
              show1=$col1")"${zName[col1]}
              echo "                  $show1"
            done
            break
            ;;
        7 ) ###################################### DISPLAY 2 COLUMNS ###################
            zoffset1=$(( ( zcount + 1 ) / 2 - 1 ))
            fat=20
            for col1 in $(eval echo {0..$zoffset1})
            do
              col2=$(( col1 + zoffset1 + 1 ))
              show1=$col1")"${zName[col1]}"                        "
              show1=${show1:0:$fat}
              show2=$col2")"${zName[col2]}
              show2=${show2:0:$fat}
              test=`echo $show2`
              test=${test#*)}
              if [[ ${#test} -eq 0 ]] ; then
                show2=
              fi
              echo "        $show1  $show2"
            done
            break
            ;;
        1|11|15|17|18 ) ########################## DISPLAY 3 COLUMNS ###################
            zoffset1=$(( ( zcount + 2 ) / 3 - 1 ))
            fat=21
            for col1 in $(eval echo {0..$zoffset1})
            do
              col2=$(( col1 + zoffset1 + 1 ))
              col3=$(( col2 + zoffset1 + 1 ))
              col4=$(( col3 + zoffset1 + 1 ))
              show1=$col1")"${zName[col1]}"                        "
              show1=${show1:0:$fat}
              show2=$col2")"${zName[col2]}"                        "
              show2=${show2:0:$fat}
              show3=$col3")"${zName[col3]}
              show3=${show3:0:$fat}
              test=`echo $show3`
              test=${test#*)}
              if [[ ${#test} -eq 0 ]] ; then
                show3=
                test=`echo $show2`
                test=${test#*)}
                if [[ ${#test} -eq 0 ]] ; then
                  show2=
                fi
              fi
              echo "  $show1 $show2 $show3"
            done
            break
            ;;
        5|12 ) ################################### DISPLAY 4 COLUMNS ###################
            zoffset1=$(( ( zcount + 3 ) / 4 - 1 ))
            fat=18
            for col1 in $(eval echo {0..$zoffset1})
            do
              col2=$(( col1 + zoffset1 + 1 ))
              col3=$(( col2 + zoffset1 + 1 ))
              col4=$(( col3 + zoffset1 + 1 ))
              show1=$col1")"${zName[col1]}"                        "
              show1=${show1:0:$fat}
              show2=$col2")"${zName[col2]}"                        "
              show2=${show2:0:$fat}
              show3=$col3")"${zName[col3]}"                        "
              show3=${show3:0:$fat}
              show4=$col4")"${zName[col4]}
              show4=${show4:0:$fat}
              test=`echo $show4`
              test=${test#*)}
              if [[ ${#test} -eq 0 ]] ; then
                show4=
                test=`echo $show3`
                test=${test#*)}
                if [[ ${#test} -eq 0 ]] ; then
                  show3=
                  test=`echo $show2`
                  test=${test#*)}
                  if [[ ${#test} -eq 0 ]] ; then
                    show2=
                  fi
                fi
              fi
              echo "  $show1 $show2 $show3 $show4"
            done
            break
            ;;
        2 ) ################################### DISPLAY 2 PAGE 4 COLUMNS ###############
            zoffset1=$(( ( zcount + 3 ) / 4 - 1 ))
            zset1=$(( zcount / 8 ))
            fat=18
            for col1 in $(eval echo {0..$zset1})
            do
              col2=$(( col1 + zoffset1 + 1 ))
              col3=$(( col2 + zoffset1 + 1 ))
              col4=$(( col3 + zoffset1 + 1 ))
              show1=$col1")"${zName[col1]}"                        "
              show1=${show1:0:$fat}
              show2=$col2")"${zName[col2]}"                        "
              show2=${show2:0:$fat}
              show3=$col3")"${zName[col3]}"                        "
              show3=${show3:0:$fat}
              show4=$col4")"${zName[col4]}
              show4=${show4:0:$fat}
              test=`echo $show4`
              test=${test#*)}
              if [[ ${#test} -eq 0 ]] ; then
                show4=
                test=`echo $show3`
                test=${test#*)}
                if [[ ${#test} -eq 0 ]] ; then
                  show3=
                  test=`echo $show2`
                  test=${test#*)}
                  if [[ ${#test} -eq 0 ]] ; then
                    show2=
                  fi
                fi
              fi
              echo "  $show1 $show2 $show3 $show4"
            done
            echo ""
            echo -n "    Press Enter for next page or, pick by number: "
            read zonePick
            if [[ ${#zonePick} -eq 0 ]] ; then
              clear
              echo ""
              echo "$header"
              echo "                    ('q' to quit)"
              echo ""
              zset1=$(( zset1 + 1 ))
              for col1 in $(eval echo {$zset1..$zoffset1})
              do
                col2=$(( col1 + zoffset1 + 1 ))
                col3=$(( col2 + zoffset1 + 1 ))
                col4=$(( col3 + zoffset1 + 1 ))
                show1=$col1")"${zName[col1]}"                        "
                show1=${show1:0:$fat}
                show2=$col2")"${zName[col2]}"                        "
                show2=${show2:0:$fat}
                show3=$col3")"${zName[col3]}"                        "
                show3=${show3:0:$fat}
                show4=$col4")"${zName[col4]}
                show4=${show4:0:$fat}
                test=`echo $show4`
                test=${test#*)}
                if [[ ${#test} -eq 0 ]] ; then
                  show4=
                  test=`echo $show3`
                  test=${test#*)}
                  if [[ ${#test} -eq 0 ]] ; then
                    show3=
                    test=`echo $show2`
                    test=${test#*)}
                    if [[ ${#test} -eq 0 ]] ; then
                      show2=
                    fi
                  fi
                fi
                echo "  $show1 $show2 $show3 $show4"
              done
            fi
            break
            ;;
        Q|q ) ################################ QUIT AND ABORT EXECUTION ################
            clear
            echo " Timezone modification aborted."
            exit 0
            ;;
        * ) ############################### CIRCLE ON BAD SELECTION INPUTS #############
            clear
            echo "              Please make a valid selection!"
            read -t2 junk
            ;;
      esac
    fi
    region=
  done
 
  if [[ ${#zonePick} -eq 0 ]] ; then
    echo ""
    echo -n "                  Pick by number: "
    read zonePick
  fi
 
  # PATH OF SELECTED "ZONE FILE" LOCATED INSIDE > >  ${tzFile[$zonePick]}
 
  if echo $zonePick | grep -iq q 2> /dev/null; then
    clear
    echo " Timezone modification aborted."
    exit 0
  fi
  if [[ ${#zonePick} -ne 0 ]] ; then                    # not zero length
    if [ $zonePick -eq $zonePick 2> /dev/null ]; then  # is a whole number
      zcount=$(( zcount - 1 ))
      if [[ $zonePick -gt $zcount ]] ; then            # is out of range
        clear
        echo "              Please make a valid selection!"
        read -t2 junk
      else                                              # ODDS ARE WE HAVE A VALID SELECTION
        echo "Your selection was:  ${tzFile[$zonePick]:20}"
        #  Shuffle files into place as needed to apply new selection
        mv -f /etc/localtime /etc/localtime-old
        cp -rf ${tzFile[$zonePick]} /etc/localtime
        echo ""
        echo "Please verify timezone and re-adjust if necessary:"
        echo -n "      "
        date
        echo ""
        exit 0
      fi
    else                                                # user does not understand "Pick by number"
      clear
      echo "                You must select by number!"
      read -t2 junk
    fi
  fi
done

6. Adjust permissions after you save the file so the user must be logged in root to use the script (same as the old line command version).
Code:

            chmod 700 timeconfig
7. You're done. Just give it a spin at the root system prompt. Change directory first to verify it will launch as easily as the original utility without a path prefix:
Code:

            cd /
            timeconfig

Infinitely better than the 'timeconfig' that comes with CentOS 6.x, since there is no command line version of it in CentOS 6.x

Available zone selections are not as complete as the original 'timeconfig' but should be adequate to suit most needs. More than suits mine. Also, I'm no linux bash scripting guru so some of my techniques could undoubtedly be improved upon. Feel free to use 'as is' or modify to your liking, and either way have fun.

trickykid 01-24-2014 08:12 PM

What's wrong with just the `date` command? They likely abandoned timeconfig as probably most just use NTP nowadays, no reason to maintain a command when there's already tools available that work just as easily.

Pick your timezone:

Code:

ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime
Once you get your correct timezone, use NTP. If it's totally off or no NTP is available, date is fairly easy to use. man date for more info. It's pretty basic if you ask me.

Cheers.

RandyTech 01-24-2014 09:45 PM

Quote:

Originally Posted by trickykid (Post 5104746)
What's wrong with...

Hi trickykid, and thanks for sharing with sample code!!

Nothing wrong with having "more than one way to skin a cat".
Nothing wrong with enhancing a product and calling it "an upgrade".

What is wrong is when *anyone* enhances a product, strips it of features *they* deem "unworthy to maintain" and then presume to call it an *upgrade*, which is it not -- not after stripping it of *any* features. That's just plain wrong. When something is enhanced, it is said to be upgraded. When something is lost, it is said to be *degraded*. Calling Centos 6 an upgrade over Centos 5 is just flat ignorant (if not brian dead), as it was to refer to Centos 5 an upgrade over Centos 4 after they removed the 'printconf' tui. These could legitimately be presented as a *substitute* products and that would be fine. "Upgrades", they are not. (rant rant rant :D )

Anyway, the software company I work for ships pre-configured drives to customers around the world, and you are correct. Some may not have internet access. Regardless of our reason which is nobody's business but our own (hint hint >> certainly not for Centos/RHEL to decide worthy or not), it is extremely convenient for us to have the ancient and traditional 'timeconfig' available as opposed to having to go filtering through directories manually trying to find the proper source file to suit the customer to fit into a new command syntax we never had to concern ourselves with previously.

Not knocking your alternative solution, and again, very kind of you to provide sample code. Obviously less bother than setting up my script if its a one shot deal so it is indeed a worthy contribution to this thread! :)

loadedmind 02-04-2014 08:21 AM

Both useful information for time setup. We work in a lab with an isolated network (no outside access) and currently have chrony to replace NTP. The configuration consists of a server and clients whereby the clock from the main server is used to provide accurate time. Some what-if's:

* What if the clock on the server goes bad?
* What if the server goes down?
* What if drift causes exponential accuracy of time to fail?

Thankfully, chrony can address all of these. Check here for all the information you'll ever need on chrony:
http://chrony.tuxfamily.org/

For the OP's particular scenario, if I were in his shoes, I'd definitely do something very similar to address shipping out machines that need the correct time where one can't know whether the system will go into a "connected" environment or not.

ultimatebob 08-25-2016 08:40 AM

I found that this awesome timeconfig.sh script written by RandyTech has a bug where it's not updating the /etc/sysconfig/clock file. That's a problem, because that file is used when the tzdata package is updated via yum. Basically, it reads the old time zone from the configuration file that wasn't updated, and resets the time zone back to the default value. There is a bug written up for this in the CentOS Mantis tracking system, but it was never fixed.

I was able to fix this by updating the script as follows:

Code:


#!/bin/sh
#
clear
trap '' 2
until [ "$question" = "Where is the Loop?" ]; do
  region=
  zonePick=
  until [ "$answer" = "42" ]; do
    clear
    echo -n "            "
    date
    echo "    ***********  PICK WORLD REGION ***********"
    echo "                    ('q' to quit)"
    echo ""
    count=`find /usr/share/zoneinfo/ -maxdepth 1 -type d |wc -l`
    dirName=(". "`find /usr/share/zoneinfo/ -maxdepth 1 -type d |sort |sed -e 's%^.*/%%'`)
    tzDir=(`find /usr/share/zoneinfo/ -maxdepth 1 -type d |sort`)
    offset=$(( count / 2 ))
    count=$(( count - 1 ))
   
    for left in $(eval echo {1..$offset})
    do
      right=$(( left + offset ))
      ldummy=$left")"${dirName[left]}"                        "
      ldummy=${ldummy:0:25}
      rdummy=$right")"${dirName[right]}
      echo "        $ldummy  $rdummy"
    done
    echo ""
    echo -n "                  Pick a region: "
    read region
    if [[ ! $region = "" ]] ; then # Process user region
      header="    ************ PICK '"${tzDir[$region]:20}"' ZONE ************"
      clear
      echo ""
      echo "$header"
      echo "                    ('q' to quit)"
      zcount=`find ${tzDir[$region]} -maxdepth 1 -type f |wc -l`
      zName=(`find ${tzDir[$region]} -maxdepth 1 -type f |sort |sed -e 's%^.*/%%'`)
      tzFile=(`find ${tzDir[$region]} -maxdepth 1 -type f |sort`)
      echo ""
      case $region in
        3|4|6|8|9|10|13|14|16 ) ################## DISPLAY 1 COLUMN ####################
            zcount=$(( zcount - 1 ))
            for col1 in $(eval echo {0..$zcount})
            do
              show1=$col1")"${zName[col1]}
              echo "                  $show1"
            done
            break
            ;;
        7 ) ###################################### DISPLAY 2 COLUMNS ###################
            zoffset1=$(( ( zcount + 1 ) / 2 - 1 ))
            fat=20
            for col1 in $(eval echo {0..$zoffset1})
            do
              col2=$(( col1 + zoffset1 + 1 ))
              show1=$col1")"${zName[col1]}"                        "
              show1=${show1:0:$fat}
              show2=$col2")"${zName[col2]}
              show2=${show2:0:$fat}
              test=`echo $show2`
              test=${test#*)}
              if [[ ${#test} -eq 0 ]] ; then
                show2=
              fi
              echo "        $show1  $show2"
            done
            break
            ;;
        1|11|15|17|18 ) ########################## DISPLAY 3 COLUMNS ###################
            zoffset1=$(( ( zcount + 2 ) / 3 - 1 ))
            fat=21
            for col1 in $(eval echo {0..$zoffset1})
            do
              col2=$(( col1 + zoffset1 + 1 ))
              col3=$(( col2 + zoffset1 + 1 ))
              col4=$(( col3 + zoffset1 + 1 ))
              show1=$col1")"${zName[col1]}"                        "
              show1=${show1:0:$fat}
              show2=$col2")"${zName[col2]}"                        "
              show2=${show2:0:$fat}
              show3=$col3")"${zName[col3]}
              show3=${show3:0:$fat}
              test=`echo $show3`
              test=${test#*)}
              if [[ ${#test} -eq 0 ]] ; then
                show3=
                test=`echo $show2`
                test=${test#*)}
                if [[ ${#test} -eq 0 ]] ; then
                  show2=
                fi
              fi
              echo "  $show1 $show2 $show3"
            done
            break
            ;;
        5|12 ) ################################### DISPLAY 4 COLUMNS ###################
            zoffset1=$(( ( zcount + 3 ) / 4 - 1 ))
            fat=18
            for col1 in $(eval echo {0..$zoffset1})
            do
              col2=$(( col1 + zoffset1 + 1 ))
              col3=$(( col2 + zoffset1 + 1 ))
              col4=$(( col3 + zoffset1 + 1 ))
              show1=$col1")"${zName[col1]}"                        "
              show1=${show1:0:$fat}
              show2=$col2")"${zName[col2]}"                        "
              show2=${show2:0:$fat}
              show3=$col3")"${zName[col3]}"                        "
              show3=${show3:0:$fat}
              show4=$col4")"${zName[col4]}
              show4=${show4:0:$fat}
              test=`echo $show4`
              test=${test#*)}
              if [[ ${#test} -eq 0 ]] ; then
                show4=
                test=`echo $show3`
                test=${test#*)}
                if [[ ${#test} -eq 0 ]] ; then
                  show3=
                  test=`echo $show2`
                  test=${test#*)}
                  if [[ ${#test} -eq 0 ]] ; then
                    show2=
                  fi
                fi
              fi
              echo "  $show1 $show2 $show3 $show4"
            done
            break
            ;;
        2 ) ################################### DISPLAY 2 PAGE 4 COLUMNS ###############
            zoffset1=$(( ( zcount + 3 ) / 4 - 1 ))
            zset1=$(( zcount / 8 ))
            fat=18
            for col1 in $(eval echo {0..$zset1})
            do
              col2=$(( col1 + zoffset1 + 1 ))
              col3=$(( col2 + zoffset1 + 1 ))
              col4=$(( col3 + zoffset1 + 1 ))
              show1=$col1")"${zName[col1]}"                        "
              show1=${show1:0:$fat}
              show2=$col2")"${zName[col2]}"                        "
              show2=${show2:0:$fat}
              show3=$col3")"${zName[col3]}"                        "
              show3=${show3:0:$fat}
              show4=$col4")"${zName[col4]}
              show4=${show4:0:$fat}
              test=`echo $show4`
              test=${test#*)}
              if [[ ${#test} -eq 0 ]] ; then
                show4=
                test=`echo $show3`
                test=${test#*)}
                if [[ ${#test} -eq 0 ]] ; then
                  show3=
                  test=`echo $show2`
                  test=${test#*)}
                  if [[ ${#test} -eq 0 ]] ; then
                    show2=
                  fi
                fi
              fi
              echo "  $show1 $show2 $show3 $show4"
            done
            echo ""
            echo -n "    Press Enter for next page or, pick by number: "
            read zonePick
            if [[ ${#zonePick} -eq 0 ]] ; then
              clear
              echo ""
              echo "$header"
              echo "                    ('q' to quit)"
              echo ""
              zset1=$(( zset1 + 1 ))
              for col1 in $(eval echo {$zset1..$zoffset1})
              do
                col2=$(( col1 + zoffset1 + 1 ))
                col3=$(( col2 + zoffset1 + 1 ))
                col4=$(( col3 + zoffset1 + 1 ))
                show1=$col1")"${zName[col1]}"                        "
                show1=${show1:0:$fat}
                show2=$col2")"${zName[col2]}"                        "
                show2=${show2:0:$fat}
                show3=$col3")"${zName[col3]}"                        "
                show3=${show3:0:$fat}
                show4=$col4")"${zName[col4]}
                show4=${show4:0:$fat}
                test=`echo $show4`
                test=${test#*)}
                if [[ ${#test} -eq 0 ]] ; then
                  show4=
                  test=`echo $show3`
                  test=${test#*)}
                  if [[ ${#test} -eq 0 ]] ; then
                    show3=
                    test=`echo $show2`
                    test=${test#*)}
                    if [[ ${#test} -eq 0 ]] ; then
                      show2=
                    fi
                  fi
                fi
                echo "  $show1 $show2 $show3 $show4"
              done
            fi
            break
            ;;
        Q|q ) ################################ QUIT AND ABORT EXECUTION ################
            clear
            echo " Timezone modification aborted."
            exit 0
            ;;
        * ) ############################### CIRCLE ON BAD SELECTION INPUTS #############
            clear
            echo "              Please make a valid selection!"
            read -t2 junk
            ;;
      esac
    fi
    region=
  done
 
  if [[ ${#zonePick} -eq 0 ]] ; then
    echo ""
    echo -n "                  Pick by number: "
    read zonePick
  fi
 
  # PATH OF SELECTED "ZONE FILE" LOCATED INSIDE > >  ${tzFile[$zonePick]}
 
  if echo $zonePick | grep -iq q 2> /dev/null; then
    clear
    echo " Timezone modification aborted."
    exit 0
  fi
  if [[ ${#zonePick} -ne 0 ]] ; then                    # not zero length
    if [ $zonePick -eq $zonePick 2> /dev/null ]; then  # is a whole number
      zcount=$(( zcount - 1 ))
      if [[ $zonePick -gt $zcount ]] ; then            # is out of range
        clear
        echo "              Please make a valid selection!"
        read -t2 junk
      else                                              # ODDS ARE WE HAVE A VALID SELECTION
        echo "Your selection was:  ${tzFile[$zonePick]:20}"
        #  Shuffle files into place as needed to apply new selection
        mv -f /etc/localtime /etc/localtime-old
        cp -rf ${tzFile[$zonePick]} /etc/localtime
                mv -f /etc/sysconfig/clock /etc/sysconfig/clock-old
                {
                echo ZONE=\"${tzFile[$zonePick]:20}\"
                } > /etc/sysconfig/clock
        echo ""
        echo "Please verify timezone and re-adjust if necessary:"
        echo -n "      "
        date
        echo ""
        exit 0
      fi
    else                                                # user does not understand "Pick by number"
      clear
      echo "                You must select by number!"
      read -t2 junk
    fi
  fi
done


RandyTech 08-25-2016 01:38 PM

@LoadedMind -- Nice contribution to the thread, complete with reference link -- very nice!

@UltimateBob -- Excellent catch. Thanks for sharing the code adjustment!


All times are GMT -5. The time now is 12:17 PM.