0

My computer's battery is bad, but I don't really want to spend the money to replace it because it still lasts several hours. But when it gets down to about 10%, it sometimes just completely powers down the computer without warning. When this happens the internal clock is reset to December 31, 2000. I don't think the clock is maintained by a watch battery like in PC's, otherwise I'd just change that.

Anyhow, once the OS loads, I get a warning that the date is set to before 2008 and that some applications may fail to function properly, my WiFi being one of them. I then need to perform a few steps to change the time, save it, log off and log back on. It's just a minor pain in the butt, but I'd like to have a script similar to a PC's autoexec.bat file do the work for me.

During boot up I'd like to have a script check the time. If it's before 2008, then set it to a date beyond 2008. As long as the date is after January 1, 2008 then my WiFi will work and the system will automatically correct the time in a few moments.

I'm running OSX 10.6.8 on an Intel based Mac.

Thanks in advance for your help, Pete

2
  • 1
    I'll let you make the script since you haven't told us what you've tried, but for what you need to run on boot - crontab would probably be the easiest. In a terminal type crontab -e and for the command, you'll want @reboot /path/to/my/script. Note: Since on some *nix environments, time is a sudo right - you may need to do that from a root terminal.
    – nerdwaller
    Sep 24, 2013 at 5:32
  • I haven't tried anything. Unlike Windows based PCs, I know absolutely nothing about Macs and I couldn't really find much help googling it. I don't know what file to edit nor do I know the the syntax for the script, but that I could probably figure out. That crontab command, will it make my file run on every reboot or just the next reboot. I want to run this check every time. Root terminal??? Thanks. Sep 24, 2013 at 5:39

1 Answer 1

0

Scheduling

To run a command on a schedule, your best bet is probably using crontab

Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word "chronos", Greek for "time". Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates.

So, you can set up scripts and/or programs, etc. to execute on a schedule with this. For your uses, the most applicable timer is @reboot which will run on each reboot. The actual syntax for this would be as follows:

@reboot /path/to/my/script

Since on some unixy systems, date controlling stuff is root - you may need to edit crontab from a root terminal - so do this (you need to have admin/sudo rights):

  1. Open a Terminal
  2. type sudo -i or su -
  3. Type crontab -e (that opens crontab in your environmental text editor
  4. Put the above on its own line (@reboot /path/to/my/script)
  5. Exit and Save (the actual commands depend on which editor you use)
  6. Make sure that the script is marked executable (chmod +x /path/to/my/script)

The Actual Script

So, like I said above - I'd like to see you do some of the footwork on that since we aren't a script writing service, but I may as well give you a start.

You can use the date command with parameters to get the current system time - you could do that with date '+%Y'

So here is a comparison based on year:

#!/usr/bin/env bash

sysYr=$(date '+%Y')
cutoff=2008

if [[ $sysYr -lt $cutoff ]]; then
    echo "It's before ${cutoff}!"
else
   echo "It's ${cutoff} or greater!"
fi

Breakdown of the above:

  1. This just tells the system where to look for the program that runs the script, this is more portable than /usr/bin/bash as used to be common. I believe it also works on mac. If not, type which bash and you can point directly to it instead.
  2. The $( something ) is command substitution which replaces the command with the output of the command. In this case, we are running date '+%Y' which will expand out (currently) to the integer 2013
  3. Setting another variable for the cutoff, just for good code sake.
  4. Compare and do conditional commands. Generally, it is best to quote items being compared to protect against spaces and such; however, these are numerals and there is no reason they shouldn't be, also -lt is the bash comparison for a numeral "less than".
  5. The rest is fairly self explanatory - echo displays the following text.
1
  • 1
    Hey nerdwaller, thanks so much for your help. Your answer was clear and concise, and most importantly it WORKS!!! :) I learned so much about Apple Scripting during the past few days, but it's becoming habitual. Sep 27, 2013 at 6:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .