Advertisement
Guest User

[BASH] KeepTime on OSX with Dead Battery

a guest
Mar 10th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. # This script was designed to keep time for Laptop's running *NIX systems with dead CMOS batteries
  5. # Specfically Mac OSX but it can be adapted to other *NIX Systems, use a launch daemon to run on osx
  6.  
  7. #Written By:
  8. #Geofferey A. Eakins
  9.  
  10.  
  11. #Set up directories & create .conf file
  12.  
  13. if [ $(find /usr/share/fixes | wc -l ) -eq 0 ]; then
  14. echo "";
  15. echo "Setting up directories & creating .conf file...";
  16. sleep 1;
  17. mkdir -p /usr/share/fixes/time;
  18. cp ./set-time /usr/share/fixes/time;
  19. cp ./com.geofferey.keeptime.plist /Library/LaunchDaemons/com.geofferey.keeptime.plist;
  20. chmod 0755 /usr/share/fixes/time/set-time;
  21. ln -s /usr/share/fixes/time/set-time /usr/bin/set-time;
  22. chmod 0644 /Library/LaunchDaemons/com.geofferey.keeptime.plist;
  23. fi
  24.  
  25. if [ $(find /usr/share/fixes/time -name time.conf | wc -l) -eq 0 ]; then
  26. touch /usr/share/fixes/time/time.conf;
  27. echo "SAVEDTIME=" >> /usr/share/fixes/time/time.conf
  28. fi
  29.  
  30.  
  31. #Use variables from .conf file
  32.  
  33. . /usr/share/fixes/time/time.conf
  34.  
  35.  
  36. #Variables
  37.  
  38. DATE=$(date +%m%d%H%M%Y)
  39. YEAR=$(date +%Y)
  40.  
  41.  
  42. #Check year & set time from variable in .conf file
  43.  
  44. echo ""
  45. echo "Checking year and setting date/time if reset..."
  46. sleep 1
  47.  
  48. [ $YEAR -lt 2014 ] && date $SAVEDTIME
  49.  
  50.  
  51. #Check for active connection to internet & continue script if pass or fails
  52.  
  53. echo ""
  54. echo "Checking for active internet connection before continuing..."
  55. sleep 1
  56.  
  57. for i in {1..1000};
  58. do ping -c1 www.google.com > /dev/null 2> /dev/null  && break;
  59. done
  60.  
  61.  
  62. #Update time via ntp server
  63.  
  64. echo ""
  65. echo "Updating time via ntp server..."
  66. sleep 1
  67.  
  68. ntpdate -u $(systemsetup -getnetworktimeserver|awk '{print $4}') > /dev/null 2> /dev/null
  69.  
  70.  
  71. #Save time to variable in .conf file if year is greater than 2001
  72.  
  73. echo ""
  74. echo "Saving date & time to file..."
  75. sleep 1
  76.  
  77. if [ $YEAR -gt 2001 ] ; then  
  78. sed -i -e "s/^SAVEDTIME=.*/SAVEDTIME=$DATE/" /usr/share/fixes/time/time.conf &&
  79. rm /usr/share/fixes/time/time.conf-e
  80. fi
  81.  
  82. echo ""
  83. sleep 1
  84. echo "Done!"
  85. echo ""
  86.  
  87.  
  88. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement