crontab -e to schedule commands in OS X

Back in my last posting I spoke about backups and how you can use rsync to back things up reliably in a more complete way than finder. In spirit of backup day I will elaborate on scheduling this as REGULAR backups are really the only backups that save us the suffering of lost data.

This time we will take a look at crontab which stands for cron table. It runs daily, weekly and monthly on respective schedules. You can add commands to be run daily and they will be executed at the time scheduled or if your computer is sleeping, the next time you wake it. Using crontab is as simple as taking the commands you want to schedule and putting them into a plain text document with .command as the extension. I will use the rsync command here. Open terminal and punch in:

cd ~/Documents

You will now be in the Documents folder of your home user.

Now punch in:

touch rsyncbackup.command

The touch command creates a blank file.

Then enter: vi rsyncbackup.command

This will bring you into a very archaic text editor that you will be forced to use on some versions of linux, it’s best to just accept it and learn how to get around in it as it will save you a hell of a lot of trouble. First of all using the esc key then entering colon allows you to enter a slew of commands. Just stick to what I am using here and you will be fine 😉 Now to edit the cron table we need to enter:

crontab -e

Now to enter text we need to hit the i key to enter insert mode, then type:

30 23 * * * ~/Documents/rsyncbackup.command

cron works on a 24 clock. Another thing, the file we just created with touch has no permissions for execution by the current user. Permissions lie at the core of security in *nix based operating systems. To make it executable enter:

chmod u+x rsyncbackup.command

Now the command should run every day at that time. The fields and acceptable values are in this order:

minute        0-59

hour          0-23

day of month    1-31

month         1-12 (or names, see below)

day of week     0-7 (0 or 7 is Sun, or use names)

Also a good tip to keep track of the sync is to add a file in the home folder of the current user with nothing in it except for your email address. In terminal again enter:

touch ~/.forward

vi  ~/.forward

enter your email address

press esc

:wq

You will now receive an automatic email from the mail application on that system. This whole thing works really well if your the only user. If it’s a more complicated backup scenario you really need to plan it out for crontab to work.  Happy backup day!

 

J

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *