Mark2020H

Bash Time setting and sync RPI VoidRealms Solved

Apr 29th, 2020
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.78 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # script MD Harrington 2 May 2020
  4. # Time 00:05hrs
  5.  
  6. # Variable name reminders
  7.  
  8. #
  9. #   $0 - The name of the Bash script.
  10. #   $1 - $9 - The first 9 arguments to the Bash script. (As mentioned above.)
  11. #   $# - How many arguments were passed to the Bash script.
  12. #   $@ - All the arguments supplied to the Bash script.
  13. #   $? - The exit status of the most recently run process.
  14. #   $$ - The process ID of the current script.
  15. #   $USER - The username of the user running the script.
  16. #   $HOSTNAME - The hostname of the machine the script is running on.
  17. #   $SECONDS - The number of seconds since the script was started.
  18. #   $RANDOM - Returns a different random number each time is it referred to.
  19. #   $LINENO - Returns the current line number in the Bash script.
  20.  
  21.  
  22.  
  23.  
  24. STD='\033[0;0;39m'        #reset
  25. IRed='\033[0;91m'         # Red
  26. IGreen='\033[0;92m'       # Green
  27. IYellow='\033[0;93m'      # Yellow
  28. IBlue='\033[0;94m'        # Blue
  29. IPurple='\033[0;95m'      # Purple
  30. ICyan='\033[0;96m'        # Cyan
  31. IWhite='\033[0;97m'       # White
  32.  
  33. #global variable
  34. CURRENTDATEONLY=`date +"%A, %b %d, %Y %H:%M:%S"`
  35.  
  36. #important must have this otherwise no go
  37.  
  38. FILE=/etc/ntp.conf
  39. FILEBCK=/etc/ntp.conf.back
  40.  
  41. timedatectl | grep Time
  42.  
  43.  
  44.  
  45.  
  46. show_menus() {
  47.    
  48.     clear
  49.     echo -e "   ${IRed}**********************************************${STD}"   
  50.     echo -e "   ${IRed}*  Date : ${CURRENTDATEONLY}     *${STD}"
  51.     echo -e "   ${IRed}*   Timezone and Synchronize System Clock    *${STD}"
  52.     echo -e "   ${IRed}*              Bash Menu Driven              *${STD}"
  53.     echo -e "   ${IRed}*          Written by MD Harrington          *${STD}"
  54.     echo -e "   ${IRed}*                                            *${STD}"
  55.     echo -e "   ${IRed}**********************************************${STD}"
  56.     echo -e "   ${IGreen}1. Show Current Set Time Zone ${STD}"
  57.     echo -e "   ${IPurple}2. Set Time Now {STD}"
  58.     echo -e "   ${IBlue}3. Check Current Time and Date Status${STD}"
  59.     echo -e "   ${IRed}4. NTP synchronized ${STD}"
  60.     echo -e "   ${ICyan}5. Use Local TZ NTP Not Synced ${STD}"
  61.     echo -e "   ${IWhite}6. Exit ${STD}"
  62. }
  63.  
  64.  
  65.  
  66. pause(){
  67.    
  68.   read -p "Press [Enter] to continue..." p_EnterKey
  69. }
  70.  
  71. GetCurrentTimeZone()
  72. {  
  73.     echo "Time Zone is set to "
  74.     echo ""
  75.     timedatectl | grep Time
  76.     pause
  77.    
  78. }
  79.  
  80.  
  81. SetTimeNow()
  82. {
  83.    echo -e  "${IGreen}Set time  for NOW ${STD}"
  84.    sudo systemctl stop  systemd-timesyncd.service
  85.    sudo timedatectl set-local-rtc 1
  86.    sudo timedatectl set-ntp false
  87.    read -p "Input time duration of simulation in HH:MM:SS format " -e Time
  88.  
  89.     while :; do
  90.         if [[ $Time =~ ^([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then
  91.             if (( BASH_REMATCH[3] < 60 ))\
  92.             && (( BASH_REMATCH[2] < 60 ))\
  93.             && (( BASH_REMATCH[1] < 24 )); then
  94.             break
  95.         fi
  96.     fi
  97.    read -p "Wrong format. Please use the HH:MM:SS format " -e Time
  98.    done
  99.  
  100. # Set Time with $Time  
  101.  timedatectl set-time $Time
  102.  
  103.  echo "Self check to see if time was set correctly "
  104.  
  105.  timedatectl  
  106.  
  107.  pause
  108.  
  109.  timedatectl set-ntp true
  110.  systemctl restart  systemd-timesyncd.service
  111.  
  112.    
  113. }
  114.  
  115. checkstatus()
  116. {
  117.     echo -e  "${IPurple}Checking time and date status${STD}"
  118.     timedatectl status
  119.    
  120.    
  121.     pause
  122.    
  123. }
  124.  
  125. synchNTP()
  126. {
  127.     echo -e  "${IYellow}Synchronize NTP${STD} "
  128.     sudo systemctl stop  systemd-timesyncd.service
  129.     sudo timedatectl set-local-rtc 0
  130.  
  131.     timedatectl set-ntp true
  132.    
  133.     sudo systemctl restart  systemd-timesyncd.service  
  134.    
  135.     echo -e "${IYellow}You may need to reboot the Raspberry Pi to apply this change${STD} "
  136.     timedatectl status
  137.     pause
  138.    
  139. }
  140.  
  141. UseLocalTZ()
  142. {
  143.     echo -e  "${ICyan}NB !! Using Local TZ and  not synced with NTP server ${STD} "
  144.     sudo systemctl stop  systemd-timesyncd.service
  145.     sudo timedatectl set-local-rtc 1
  146.    
  147.     sudo systemctl restart  systemd-timesyncd.service  
  148.     timedatectl status
  149.     pause
  150.    
  151. }
  152.  
  153. QuitMenu()
  154. {
  155.     echo ""
  156.     echo ""
  157.     echo -e "   ${ICyan}**********************************************${STD}"
  158.     echo -e "   ${ICyan}*      Thank you for using this menu         *${STD}"
  159.     echo -e "   ${ICyan}*         Mark David Harrington              *${STD}"
  160.     echo -e "   ${ICyan}**********************************************${STD}"
  161.    
  162.         sleep 1
  163.           clear
  164.             exit 0
  165. }
  166.  
  167.  
  168.  
  169. read_options(){
  170.    
  171.     local choice
  172.     read -p "Enter choice [ 1 - 6] " choice
  173.     case $choice in
  174.         1) GetCurrentTimeZone;;
  175.         2) SetTimeNow ;;
  176.         3) checkstatus ;;
  177.         4) synchNTP;;
  178.         5) UseLocalTZ ;;
  179.         6) QuitMenu ;;
  180.         *) echo -e "${RED}Error...${STD}" && sleep 2
  181.     esac
  182. }
  183.  
  184. # ----------------------------------------------
  185. #  Trap CTRL+C, CTRL+Z and quit signals
  186. # ----------------------------------------------
  187.  
  188. trap '' SIGINT SIGQUIT SIGTSTP
  189.  
  190. # -----------------------------------
  191. # Main logic - infinite loop
  192. # ------------------------------------
  193. while true
  194. do
  195.  
  196.     show_menus
  197.     read_options
  198. done
Add Comment
Please, Sign In to add comment