
Cron is one of those little programs that can change the world. It’s a scheduling progam, meaning that you can give it instructions such as ‘at 10:45am on every Monday run this program’ and it will faithfully wait until the appointed time to do what you asked it.
Why is this useful? Cron can be used to automate tasks such as backups, the running of hourly, daily weekly monthly or yearly reports, and just about anything else you can think of. This will be a basic guide to using cron. Specifically, to run the backups we discussed earlier.
In it’s basic form, Cron is told to do things by text files called crontabs, which are located in /var/spool/cron/crontabs. When Cron is launched (and every minute after that) it checks this directory for crontabs, and loads these into memory for execution at the appropriate time. If any single crontab shows a modified timestamp been modified, it checks them all to be sure nothing else has changed.
Editing a crontab is fairly easy. If you’re used to Nano, you can simply type:
crontab -e
If you’re more comfortable with a GUI editor like Gedit, you can type:
EDITOR=gedit crontab -e
Here’s where an important distinction needs to be made. Each user can have their own Crontab, including root. If you want or need to run something with root privilages, it’s a good idea to do the following:
EDITOR=gedit sudo crontab -e
If you have elevated privileges, you can also tweak other user’s crontabs by using the -u switch. Another way to modify root’s crontab would be to run:
EDITOR=gedit sudo crontab -e -u root
Crontab has several switches which are good to know right away. -e= edit, -u= user, -l= list, and -r= remove.
Being a scheduling tool, cron has to know what your schedule is going to be be. Cron determines when it’s going to run things by a series of entries that correspond to various times, days of the weeks and months. You’ll see abbreviations for these when you edit your crontab. They are as follows:
#m h dom mon dow command
That’s shorthand for Minute, Hour, Day Of Month, Month, Day of Week and then then Command you want cron to run. The ‘*’ (asterisk) stands for any time, and entering a number tells cron exactly when to do something. Cron uses a 24 hour clock. So, if I wanted to run the command foobar at 1pm every day of the week, this is what I’d tell cron:
0 13 * * * /home/username/foobar
That means Any minute, hour 13 (1pm on the 24 hour clock) any day of the month, any month, any day of the week run foobar located at /home/username/
It can take a bit to wrap your head around this structure, so we’re going to look at a few examples.
Commands can be separated by commas if you want several different times. If I wanted to run foobar every fifteen minutes my entry would look like this:
0,15,30,45 * * * * /home/username/foobar
Minutes are done from 0 to 59. Hours from 0 to 24. Days of the month from 1 to 31. Months from 1 to 12. Days of the week from 0 (Sunday) to 6 (Saturday). So if I wanted to run a command at 3am on the 1st and 15th of every month, my crontab entry would look like this:
0 3 1,15 * * /home/username/foobar
If I wanted to tell cron to run this job on the 10th to the 20th of every month at 3 am, my entry would look like this:
0 3 10-2 * * /home/username/foobar
If I wanted it to run at 3 am on Tuesday and Thursday
0 3 * * 2,4 /home/username/foobar
Notice that in all of these examples, I use the full path to the executable, which in this case is something called foobar. It’s always a good idea to use the full path to whatever you’re running as cron may become confused with your $PATH settings. Particularly if you’re running a cron job as a different use, who’s path is different from what you may be used too.
Now, we’ve gone through a whole bunch of examples on what various cron jobs in your crontabs will look like. As with regular shell scripting, a ‘#‘ in a crontab means it’s a comment and cron will ignore anything after the #.
I’ve found that the for me it’s easier book keeping to save my commands as scripts, and then have Cron execute a single script rather than a long command. You can do it however you’d like. Since this is a howto written by me however, I’m going to show you how I do it.
As we’ve already looked at using tar and rsync to back something up to a local drive, an external drive and how to do that over a network, we’re going to look at using cron to automate this task. In this example, I’m going to set up a crontab for the root user to execute an rsync command to copy my music folder once a week to an external drive, and use tar to back up my entire computer once a month. I’ll use the same examples I did on this earlier post. I’m going to execute my weekly rsyncs as my own user, and I’m going to back up my system once a month as the root user.
In my home directory (/home/arsgeek) I’m going to create a small script which will contain nothing but a comment and my rsync command. I’ve also got to make sure that my external drive is hooked up and on. Open up a terminal session and let’s make this script.
gedit musicbak.sh
In there, I’m going to paste my rsync command to back up my music directory from my FAT32 partition to my external drive.
#! /bin/bash
#back up my music folder to my external firewire drive
rsync -arvu /media/sda5/music /media/ieee1394disk/arsgeek_backup
Now save your file. We’ll have to make it executable.
chmod +x musicbak.sh
Now, let’s decided when this thing is going to run. I’m going to have Cron back up my music directory every Wednesday at noon. Why? Well, I’m usually at lunch and my laptop is idle and plugged into my dock (with my external drive). Now to edit my crontab and tell cron to do this:
EDITOR=gedit crontab -e
And then I’ll write:
#Backup my music
0 12 * * 3 /home/arsgeek/musicbak.sh
Save the file and Cron will now execute this as I have planned.
Now, I also want to do a system backup once a month. I’m going to automate this to happen on the first Monday of every month, at 11:15 am. Here again is a time when I know I’ll be at my desk, with my laptop on and my external drive active. Let’s create another small script that will contain this command.
gedit fullbackup.sh
And into this I’ll put the following.
#! /bin/bash
#Tar command to do a full system backup to my external drive. Yo.
tar cvpzf /media/ieee1394fdisk/arsgeek_wholeshebank/arsgeek.backup.tgz –exclude=â€/proc/*†–exclude=â€/lost+found/*†–exclude=â€/dev/*†–exclude=â€/mnt/*†–exclude=â€/media/*†–exclude=â€/sys/*†–exclude=â€/tmp/*†–exclude “/var/cache/apt/*†/
Note that this is one complete line, if you’re cutting and pasting this to modify it, please check that there are no line breaks.
Again we’ll make this file executable.
chmod +x fullbackup.sh
Now, we’re going to tell cron to run this but we’ll need to do it so that the root user’s crontab is the one activated, as regular old users won’t have access to everything I’m trying to back up.
sudo crontab -e
And in root’s crontab, I’ll enter the following:
#Backup my system
15 11 * * MON#1 /home/arsgeek/fullbackup.sh
Woah! Got a bit different on you there, didn’t I? Cron is really flexible, and modern crontabs can contain 3 letter (caps) abbreviations for the days. Also, what’s the ‘#1‘ portion mean? It’s an nth value – in this case I’m saying run this on Monday, the 1st one of the month. If I changed it to #2 it would run on the second Monday of every month.
You can get very specific with Cron, specifiying seconds, years and everything in between. If you’re interested in looking into Cron a bit deeper, I would start with the man pages. There are also tons of highly detailed tutorials and examples that a simple google will turn up.
There are a few GUI tools available as well to modify your crontabs. Two that I’ve experimented with are gcrontab and gnome-schedule. Both act as front ends to Cron. Perhaps it’s simply what I’m used too but I still find using the CLI in this case to be much easier.















October 23rd, 2006 at 11:56 am
For what ever Deity you think exists, get a freaking “Print this” button, plug-in, magic-elf so that your articles can be printed out in easy to read format and saved for posterity (or used as a step by step guide at a real linux terminal).
October 23rd, 2006 at 1:53 pm
VonSkippy - that comment made me laugh! We’re working on it now. I’ve got several plugins that I’ve been trying but none seem to work all that well. I’ll keep hunting though!
Hopefully I’ll have a solution in the next day or two.