Cron as it is called is a very powerful tool. It’s simply a scheduler that allows you to do a multitude of tasks at any given time. It’s been around since the beginning of time and is built into every linux, unix, bsd distribution out there. Even Microsoft has a version of Cron, albeit it’s harder to use.
This is a super simple, completely high level cron overview. It can do a lot, but I’m going to cover the basics so you can at least get going.
1. Every user has his/her own cron file. This is called a crontab.
2. To access and modify your crontab, log in as the user whos crontab you want to modify and simply type ‘crontab -e’
3. The user’s crontab may be empty, it may have things in it, etc. Here you get to modify it . Crontab uses vi as it’s editor. So you’ll need to learn a thing or two about vi. There’s a tutorial on basic vi on this blog.
So you typed it in and now you see either a blank screen in vi or you see something that might look like this:
0 2 * * 5 /root/scripts/swimminginthought.backup.s3 >> /dev/null &
0 2 * * * /root/scripts/database.dump.s3 >> /dev/null &
Looks like greek right? Well, it’s not. In fact, it couldn’t be any simpler. So here’s the explanation.
There are 6 fields in a crontab. From left to right, it goes like this.
Fields, (left to right): minute,hour,day,month,day of the week [0-6] 0 being Sunday and 6 being Saturday, path to script or command you want to execute.
* in any of the first five fields means execute every interval (depending on the field)
so if you wanted to execute a task every minute, just simply make an entry that looks like this:
* * * * * /your.script
if you wanted to execute something at 2am every day, here’s your entry
0 2 * * * /your.script
if you wanted to execute something at 2am every friday, it would be:
0 2 * * 5 /your.script
If you wanted to execute something every 2 hours on friday, the example would be:
0 */2 * * * /your.script
Pretty simple, huh?
one cron entry per line. That’s all you need to know. Once you’re done with your scripts, just save and close the file and you’re ready to go! The cron jobs are active.
**TEST YOUR SCRIPTS BEFORE PUTTING INTO CRONTAB.***