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):
- Open a Terminal
- type
sudo -i
or su -
- Type
crontab -e
(that opens crontab in your environmental text editor
- Put the above on its own line (
@reboot /path/to/my/script
)
- Exit and Save (the actual commands depend on which editor you use)
- 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:
- 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.
- 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
- Setting another variable for the cutoff, just for good code sake.
- 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".
- The rest is fairly self explanatory -
echo
displays the following text.
crontab
would probably be the easiest. In a terminal typecrontab -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.